SyncTasks 非A + Promise库 项目简介
SyncTasks 是一个非A + Promise库,可以同步解析promises这个是故意针对ES6 promise模式设计的,它在JS引擎的下一个时刻异步解析promise回调。 在许多情况下,异步解析是承诺的最安全和最容易理解的实现,但它给分辨率增加了很大的延迟,在大多数地方这是不必要的。 此外,当我们尝试使用标准ES6承诺包装IndexedDB体系结构时,它会崩溃,因为当控制权传递回主线程时,IndexedDB会关闭数据库连接。 我们开始构建NoSQLProvider并立即遇到了这个问题。 SyncTasks是解决该问题的方法,但也是异步编程问题的一种高效解决方案。 此外,我们已经使用了一个简单的可选取消机制,它也可以通过承诺解决方案进行链接(只要您通过SyncTasks承诺链接,并且不要混合使用非取消支持的承诺。)示例代码:function sendMeAStringLater(numberOfMilliseconds: number, theString: string): SyncTasks.Promise<string> {
let defer = SyncTasks.Defer<string>();
setTimeout(() => {
defer.resolve(theString);
}, numberOfMilliseconds);
return defer.promise();
}
sendMeAStringLater(500, 'hi').then(myString => {
console.log(myString);
});
// 500 ms after running this, you will end up with a new console log line, "hi".添加消除:function sendMeAStringLater(numberOfMilliseconds: number, theString: string): SyncTasks.Promise<string> {
let defer = SyncTasks.Defer<string>();
let didFinish = false;
defer.onCancel(whyWasICancelled => {
if (!didFinish) {
didFinish = true;
defer.reject(whyWasICancelled);
}
});
setTimeout(() => {
// Make sure to bail here if it's already done. If you resolve a second time, it will throw an exception, since the
// cancel already resolved it once.
if (!didFinish) {
didFinish = true;
defer.resolve(theString);
}
}, numberOfMilliseconds);
return defer.promise();
}
let promise = sendMeAStringLater(500, 'hi').then(myString => {
console.log('Success: ' + myString);
}, errString => {
console.log('Failure: ' + errString);
});
setTimeout(() => {
promise.cancel('Sorry');
}, 200);
// 200 ms after running this, you will end up with a new console log line, "Failure: Sorry". The success case will not be
// run because it was already resolved with failure. If you change the 200ms timer to 600ms, then your console will change to
// "Success: hi" because the cancellation will happen after the success already did, so the `didFinish` check will swallow it.
let defer = SyncTasks.Defer<string>();
setTimeout(() => {
defer.resolve(theString);
}, numberOfMilliseconds);
return defer.promise();
}
sendMeAStringLater(500, 'hi').then(myString => {
console.log(myString);
});
// 500 ms after running this, you will end up with a new console log line, "hi".添加消除:function sendMeAStringLater(numberOfMilliseconds: number, theString: string): SyncTasks.Promise<string> {
let defer = SyncTasks.Defer<string>();
let didFinish = false;
defer.onCancel(whyWasICancelled => {
if (!didFinish) {
didFinish = true;
defer.reject(whyWasICancelled);
}
});
setTimeout(() => {
// Make sure to bail here if it's already done. If you resolve a second time, it will throw an exception, since the
// cancel already resolved it once.
if (!didFinish) {
didFinish = true;
defer.resolve(theString);
}
}, numberOfMilliseconds);
return defer.promise();
}
let promise = sendMeAStringLater(500, 'hi').then(myString => {
console.log('Success: ' + myString);
}, errString => {
console.log('Failure: ' + errString);
});
setTimeout(() => {
promise.cancel('Sorry');
}, 200);
// 200 ms after running this, you will end up with a new console log line, "Failure: Sorry". The success case will not be
// run because it was already resolved with failure. If you change the 200ms timer to 600ms, then your console will change to
// "Success: hi" because the cancellation will happen after the success already did, so the `didFinish` check will swallow it.