, 3 promises . "". , , promises , .
let slow = new Promise((resolve) => {
setTimeout(function()
{
resolve();
}, 2000);
});
let instant = new Promise((resolve) => {
resolve();
});
let quick = new Promise((resolve) => {
setTimeout(function()
{
resolve();
}, 1000);
});
instant.then(function(results) {
console.log("instant");
}).then(function(){return quick;}).then(function(results) {
console.log("quick");
}).then(function(){return slow;}).then(function(results) {
console.log("slow");
}).then(function(){ return Promise.all([slow, instant, quick]);}).then(function(results) {
console.log('finished');
}).catch(function(error) {
console.log(error);
});
Hide resultThis ensures that you will handle permissions in order.
Note. . In your example, you are using setTimeout, which is guaranteed to be called by handlers in order of time, so your existing code will already write "instant", "fast", "slow", "finished". The code I provided guarantees this order for any set of promises with different resolution times.
tcooc source
share