承诺解决订单并等待

时间:2017-09-16 13:25:13

标签: javascript node.js google-chrome promise async-await

我已经得到了这段代码:



af = async () => {
    Promise.resolve()
    .then(x => console.log("first then"))
    .then(x => console.log("second then"))
    await Promise.resolve()
    console.log("after await")
}

af()




在谷歌浏览器版本61.0.3163.91(官方版本)(64位) 当我从<script>运行它时,我得到了以下结果:

first then 
after await
second then

但是当我将代码复制/粘贴到控制台并运行它时,我得到了:

first then
second then
after await

node.js 8.5.0表现相似。

什么是正确的顺序以及为什么会有差异?

//编辑

另一个有趣的事情是firefox 55.0.2(64位) 对于此代码:

&#13;
&#13;
af = async () => {
    Promise.resolve()
    .then(x => console.log("first then"))
    .then(x => console.log("second then"))
    .then(x => console.log("third then"))
    .then(x => console.log("forth then"))
    await Promise.resolve()
    console.log("after await")
}

af()
&#13;
&#13;
&#13;

日志:

first then
second then
third then
after await
forth then

1 个答案:

答案 0 :(得分:0)

执行此操作的正确方法是在import numpy as np class CLUMPY: def __init__(self, k, file): # input file self.__file = file print("k=",k) print("Reading {}...".format(file)) # data points self.__points = np.loadtxt(file) # number of data points self.__n = self.__points.shape[0] # data points dimensionality (or length according to numpy terminology) self.__d = self.__points.shape[1] print("Read {}: {} points in {} dimensions.".format(file, self.__n, self.__d)) # __class[i] = j : the i-th data point is assigned to the j-th cluster self.__class = np.zeros(self.__n, dtype=np.int8) if __name__ == "__main__": clumpy = CLUMPY(2, "datasets/202d") 之前添加await,然后执行&#34;首先是&#34;然后&#34;然后&#34;然后&#34;控制台日志。这样它等待承诺完成之后才能进入控制台日志,等待#34;

这就是您的意图。

&#13;
&#13;
Promise.resolve()
&#13;
&#13;
&#13;