Promise对象
- 承诺使用 第一类表示的值,可以异步进行,将来可用
ES6 function msgAfterTimeout (msg, who, timeout) { return new Promise((resolve, reject) => { setTimeout(() => resolve(`${msg} Hello ${who}!`), timeout) }) } msgAfterTimeout("", "Foo", 100).then((msg) => msgAfterTimeout(msg, "Bar", 200) ).then((msg) => { console.log(`done after 300ms:${msg}`) }) //ES5 function msgAfterTimeout (msg, who, timeout, onDone) { setTimeout(function () { onDone(msg + " Hello " + who + "!"); }, timeout); } msgAfterTimeout("", "Foo", 100, function (msg) { msgAfterTimeout(msg, "Bar", 200, function (msg) { console.log("done after 300ms:" + msg); }); }); - 承诺组合 将一个或多个承诺合并为新的承诺,而不必关心自己的底层异步操作的排序
//ES6 function fetchAsync (url, timeout, onData, onError) { … } let fetchPromised = (url, timeout) => { return new Promise((resolve, reject) => { fetchAsync(url, timeout, resolve, reject) }) } Promise.all([ fetchPromised("http://backend/foo.txt", 500), fetchPromised("http://backend/bar.txt", 500), fetchPromised("http://backend/baz.txt", 500) ]).then((data) => { let [ foo, bar, baz ] = data console.log(`success: foo=${foo} bar=${bar} baz=${baz}`) }, (err) => { console.log(`error: ${err}`) }) //ES5 function fetchAsync (url, timeout, onData, onError) { … } function fetchAll (request, onData, onError) { var result = [], results = 0; for (var i = 0; i < request.length; i++) { result[i] = null; (function (i) { fetchAsync(request[i].url, request[i].timeout, function (data) { result[i] = data; if (++results === request.length) onData(result); }, onError); })(i); } } fetchAll([ { url: "http://backend/foo.txt", timeout: 500 }, { url: "http://backend/bar.txt", timeout: 500 }, { url: "http://backend/baz.txt", timeout: 500 } ], function (data) { var foo = data[0], bar = data[1], baz = data[2]; console.log("success: foo=" + foo + " bar=" + bar + " baz=" + baz); }, function (err) { console.log("error: " + err); });link:http://es6-features.org/#PromiseCombination
圣凡无二路,方便有多门。
浙公网安备 33010602011771号