本文整理匯總了TypeScript中es6-promise.Promise類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Promise類的具體用法?TypeScript Promise怎麽用?TypeScript Promise使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Promise類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: it
it('should convert a resolved promise to a stream', (done: any) => {
const stream = xs.from(Promise.resolve('yes'));
let nextSent = false;
stream.addListener({
next: (x: string) => {
assert.strictEqual(x, 'yes');
nextSent = true;
},
error: (err: any) => done(err),
complete: () => {
assert.strictEqual(nextSent, true);
done();
},
});
});
示例2: dispatch
.then((result) => {
dispatch({
type: GET,
data: result.data,
name: {
lastName: _.get(result.data, 'name.last', ''),
firstName: _.get(result.data, 'name.first', ''),
middleName: _.get(result.data, 'name.middle', '')
}
})
return Promise.resolve({
lastName: _.get(result.data, 'name.last', ''),
firstName: _.get(result.data, 'name.first', ''),
middleName: _.get(result.data, 'name.middle', '')
});
})
開發者ID:Emunoz88,項目名稱:node-hapi-react-redux-sass-typescript-mongo-webpack-hmr-gulp,代碼行數:16,代碼來源:actions.ts
示例3: String
return new Promise<School>((resolve, reject) => {
let ans: School = obj;
Promise.all([
course_list({school_id: String(obj.school_id)}),
person_school_list({school_id: String(obj.school_id)}),
person_comment_list({school_id: String(obj.school_id)}),
]
).then((values) => {
ans.course_list = values[0];
ans.person_school_list = values[1];
ans.person_comment_list = values[2];
resolve(ans);
}).catch((error) => {
reject(error);
})
})
示例4: it
it("should sort JSON files as text if one or more aren't JSON", function() {
let now = new Date();
let future = new Date(2100, 12, 1);
let files = [
new FileContents('test1.js', new Date(), '', JSON.stringify({ created: now })),
new FileContents('test2.js', new Date(), '', 'not JSON')
];
return Promise.resolve()
.then(() => {
return subject(files);
})
.then((result) => {
assert.isNotNull(result);
assert.equal(result.format, Format.Text);
assert.equal(result.contents, ['not JSON', JSON.stringify({ created: now })].join('\n'));
});
});
示例5: context
context('when map data is NOT present', function () {
let hiddenMapPromise: Promise<Client.JQueryDetailSelectors>;
before(function () {
const detailsWithoutMap = _.extend({}, TEST_DETAILS, { map: null });
hiddenMapPromise = createPostUpdatePromise(detailsWithoutMap);
});
after(function (done) {
revertToOriginalTestDetails(done);
});
it('should hide the map', function () {
return hiddenMapPromise.then($selectors => {
expect(elemIsHidden($selectors.$playerMap));
expect(normalize($selectors.$playerMap.text())).to.equal('');
return $selectors;
});
});
});
示例6: Promise
return new Promise((res, rej) => {
isAuth.then(a => {
if (a) {
db.run(
"DELETE FROM entries WHERE id = ?",
[entryId],
(err) => {
if (err) {
errors.push(err);
rej(errors);
} else {
res();
}
});
}
});
});
示例7: findUserIps
/**
* Attempts to find ip addresses of the user, withing the specified
* timeout.
*
* @param timeout
* @returns {Promise<IPAddress[]>} Returns a promise resolved with an
* array of ip addresses of the user on success.
*/
public static findUserIps(timeout:number = 1000):Promise<IPAddress[]> {
let RTCPeerConnection = window['RTCPeerConnection']
|| window['mozRTCPeerConnection']
|| window['webkitRTCPeerConnection'];
let rtcConfig = {
iceServers: [{urls: "stun:stun.services.mozilla.com"}]
};
if (!RTCPeerConnection) {
return Promise.reject('RTCPeerConnection not supported.');
}
return new Promise((resolve, reject) => {
let timeoutId;
let ipAddresses:IPAddress[] = [];
let peerConnection = new RTCPeerConnection(rtcConfig);
let onIceCandidate = (evt) => {
if (evt.candidate) {
let iceCandidate = evt.candidate;
let ip = WebRTCIPFinder.parseIpFromIceCandidate(iceCandidate);
if (ip != null && ipAddresses.every(other => !ip.equals(other))) {
ipAddresses.push(ip);
}
}
};
let done = (error?) => {
clearTimeout(timeoutId);
peerConnection.onicecandidate = null;
peerConnection.close();
peerConnection = null;
if (error) {
reject(error);
} else {
resolve(ipAddresses);
}
};
peerConnection.onicecandidate = onIceCandidate;
peerConnection.createDataChannel('');
peerConnection.createOffer((sdp) => {
peerConnection.setLocalDescription(sdp);
}, (err) => done(err));
timeoutId = setTimeout(done, timeout);
});
}