小编Tee*_*1er的帖子

异步JavaScript执行顺序?

异步 JS 一直让我有点困惑......

我有这个示例代码:

function asyncFunction() {
    return new Promise(function(resolve, reject) {
        resolve([1, 2, 3, 4, 5])
    })
};

function example() {
    asyncFunction().then(
        output => {
            for (element of output) {
                console.log(element + ", A") //This should be displayed first
            }
        }
    )
};

example();

console.log('B'); //But it isn't
Run Code Online (Sandbox Code Playgroud)

产生以下输出:

B
1, A
2, A
3, A
4, A
5, A
Run Code Online (Sandbox Code Playgroud)

有没有办法对此进行编程,以便在 As 之后打印 B?我实际上在这里使用了 RSS feed 解析器模块,上面只是一个例子来说明问题。

javascript asynchronous

2
推荐指数
1
解决办法
2918
查看次数

标签 统计

asynchronous ×1

javascript ×1