[英]How to get/demonstrate asynchronous behavior in JavaScript promises
Promises also ensure order-independence. 承诺还确保顺序独立。 Separating the producer and consumer, so that they can execute independently, is the crux of asynchronous programming. 将生产者和使用者分开,以便他们可以独立执行,是异步编程的关键。 With a promise, you can give a promise to a consumer before giving a resolver to a producer. 有了承诺,您可以先向消费者做出承诺,然后再向生产者提供解决方案。 They can then produce or consume on their own schedule and the promise synchronizes. 然后,他们可以按照自己的时间表生产或消费,并且诺言同步。
Trying to understand the Order Independence described above, I am writing up some code to hopefully demonstrate this. 为了理解上面描述的顺序独立性,我正在编写一些代码来希望证明这一点。 Basically, I wanted to create two asynchronous promises (based on downloading two images at two different times in partA()
and partB()
), and then resolve both using all
. 基本上,我想创建两个异步partA()
(基于在partA()
和partB()
中的两个不同时间下载两个映像),然后使用all
解决这两个问题。
var axios = require('axios');
function loadImage(imgsrc) {
return axios.get(imgsrc);
}
function partA() {
var promise1 = loadImage("http://cn.bing.com/s/a/hp_zh_cn.png");
var promise2 = setTimeout(partB,20000);
Promise.all([promise1, promise2]).then(function() {
console.log("The images are loaded");
});
};
function partB () {
return loadImage("http://cn.bing.com/s/a/hpc20.png");
};
partA();
Here are my issues and questions are: 这是我的问题,问题是:
When executing partA()
in the last line, I expected that I had to wait 20 seconds to see the success message because of the var promise2 = setTimeout(partB,20000);
在最后一行执行partA()
,由于var promise2 = setTimeout(partB,20000);
我期望我必须等待20秒才能看到成功消息var promise2 = setTimeout(partB,20000);
line (I had hoped that the two downloads happens 20 seconds apart from each other, for illustration purposes). 行(我希望两次下载间隔20秒,以供说明)。 Maybe I didn't use setTimeout
right. 也许我没有正确使用setTimeout
。 But in any case, I got the success message almost immediately after I call partA() in the babel-node
REPL. 但是无论如何,在babel- babel-node
REPL中调用partA()之后,几乎立即获得成功消息。 How do I properly get the delay? 如何正确获得延迟?
In this example (if correct), how can I interpret the Order Independence in terms of produce or consume on their own schedule
? 在此示例中(如果正确),我如何按照produce or consume on their own schedule
按produce or consume on their own schedule
来解释订单独立性? Where are the sites of production and consumption? 生产和消费地点在哪里?
(This is with babel-node 6.24.1 with --presets es2015
under Ubuntu 16.04) (这是在Ubuntu 16.04下使用带有--presets es2015
babel-node 6.24.1的)
The problem is, that setTimeout does not return a Promise. 问题是,setTimeout不返回Promise。 You have to insert a promise which executes a timeout to Promise.all. 您必须插入一个对Promise.all执行超时的Promise。
function partA() {
var promise1 = loadImage("http://cn.bing.com/s/a/hp_zh_cn.png");
var promise2 = new Promise((resolve, reject) => {
setTimeout(() => {
return resolve(partB);
}, 20000);
});
Promise.all([promise1, promise2]).then(function() {
console.log("The images are loaded");
});
};
function partB() {
return loadImage("http://cn.bing.com/s/a/hpc20.png");
};
partA();
In this case, promise2 is actually a promise, which resolves with partB result, after 20 seconds. 在这种情况下,promise2实际上是一个promise,在20秒后,它将以partB结果解析。
setTimeout
does not return a promise. setTimeout
不返回承诺。
To see order independence at work consider this example: 要查看工作中的订单独立性,请考虑以下示例:
var promise1, promise2;
promise1 = loadImage("http://cn.bing.com/s/a/hp_zh_cn.png");
promise2 = loadImage("http://cn.bing.com/s/a/hpc20.png");
promise1.then(function () {
console.log("promise1 finished");
});
promise2.then(function () {
console.log("promise2 finished");
});
Promise.all([promise1, promise2]).then(function() {
console.log("both finished");
});
The output of this script can be either: 该脚本的输出可以是:
promise1 finished
promise2 finished
both finished
or 要么
promise2 finished
promise1 finished
both finished
It just depends on which request completes earlier. 这仅取决于哪个请求较早完成。 If you want a more controlled example consider this: 如果您想要一个更受控的示例,请考虑以下事项:
var promise1, promise2;
promise1 = create_timeout_promise(20);
promise2 = create_timeout_promise(10);
function create_timeout_promise(timeout) {
var promise;
promise = new Promise(function (resolve) {
setTimeout(resolve, timeout);
});
return promise;
}
Now the exepcted output is: 现在执行的输出为:
promise2 finished
promise1 finished
both finished
Because promise 2 is going to resolve first. 因为承诺2将首先解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.