I'm having problmes in finding the use of promises and also admit that my understanding of them is very basic. From what I can tell, they just seem to mimic sync behavior.
我在寻找承诺的使用方面遇到了问题,并承认我对它们的理解是非常基础的。据我所知,他们似乎只是模仿同步行为。
Is it possible to use promises and keep the async behaviour then use then() once they have all completed?
是否有可能使用promises并保持异步行为然后在完成后使用then()?
Such as this...
比如这......
var fileRegister = [ 'fileA', 'fileB', 'fileC' ];
for( i in fileRegister ) {
asyncLoadFile( fileRegister[ i ], function( err, data ) {
delete fileRegister[ i ];
if ( ! fileRegister.length ) {
console.log('done');
}
});
}
2 个解决方案
#1
2
Yes, you can use Promise.all
to wait on multiple promises:
是的,您可以使用Promise.all等待多个承诺:
var fileRegister = [ 'fileA', 'fileB', 'fileC' ];
// returns a promise for a file
function loadFile(filename) {
return new Promise(function (resolve, reject) {
asyncLoadFile(filename, function (err, data) {
if (err) {
reject(err);
}
resolve(data);
});
});
}
Promise.all(fileRegister.map(loadFile))
.then(function (files) {
console.log("done");
});
#2
1
I'm truly surpised there seems to be no authoratative answer for this on the site. Yes, you can create a bunch of promises (each wrapping an async operation) and then call Promise,all which will resolve once they all do:
我真的很惊讶,在网站上似乎没有任何authoratative答案。是的,你可以创建一堆promises(每个包装一个异步操作),然后调用Promise,所有这些都会解决它们:
var p1 = new Promise(function(resolve, reject) {
setTimeout(resolve, 1000, "one");
});
var p2 = new Promise(function(resolve, reject) {
setTimeout(resolve, 2000, "two");
});
var p3 = new Promise(function(resolve, reject) {
setTimeout(resolve, 3000, "three");
});
var p4 = new Promise(function(resolve, reject) {
setTimeout(resolve, 4000, "four");
});
var p5 = new Promise(function(resolve, reject) {
reject("reject");
});
Promise.all([p1, p2, p3, p4, p5]).then(function(value) {
console.log(value);
}, function(reason) {
console.log(reason)
});
This is part of the E^ specification and is supported by most implementations.
这是E ^规范的一部分,并得到大多数实现的支持。
#1
2
Yes, you can use Promise.all
to wait on multiple promises:
是的,您可以使用Promise.all等待多个承诺:
var fileRegister = [ 'fileA', 'fileB', 'fileC' ];
// returns a promise for a file
function loadFile(filename) {
return new Promise(function (resolve, reject) {
asyncLoadFile(filename, function (err, data) {
if (err) {
reject(err);
}
resolve(data);
});
});
}
Promise.all(fileRegister.map(loadFile))
.then(function (files) {
console.log("done");
});
#2
1
I'm truly surpised there seems to be no authoratative answer for this on the site. Yes, you can create a bunch of promises (each wrapping an async operation) and then call Promise,all which will resolve once they all do:
我真的很惊讶,在网站上似乎没有任何authoratative答案。是的,你可以创建一堆promises(每个包装一个异步操作),然后调用Promise,所有这些都会解决它们:
var p1 = new Promise(function(resolve, reject) {
setTimeout(resolve, 1000, "one");
});
var p2 = new Promise(function(resolve, reject) {
setTimeout(resolve, 2000, "two");
});
var p3 = new Promise(function(resolve, reject) {
setTimeout(resolve, 3000, "three");
});
var p4 = new Promise(function(resolve, reject) {
setTimeout(resolve, 4000, "four");
});
var p5 = new Promise(function(resolve, reject) {
reject("reject");
});
Promise.all([p1, p2, p3, p4, p5]).then(function(value) {
console.log(value);
}, function(reason) {
console.log(reason)
});
This is part of the E^ specification and is supported by most implementations.
这是E ^规范的一部分,并得到大多数实现的支持。