Asynchronous 如何将此promise.then更改为promise.all并使用async wait?

Asynchronous 如何将此promise.then更改为promise.all并使用async wait?,asynchronous,es6-promise,Asynchronous,Es6 Promise,我在这里创建posts数组 const posts = [{ title: 'Post One', body: 'This is Post 1' }, { title: 'Post Two', body: 'This is Post 2' }, { title: 'Post Three', body: 'This is Post 3' } ] 我用Create

我在这里创建posts数组

const posts = [{
        title: 'Post One',
        body: 'This is Post 1'
    },
    {
        title: 'Post Two',
        body: 'This is Post 2'
    },
    {
        title: 'Post Three',
        body: 'This is Post 3'
    }
]
我用CreatePost创建了第四篇文章

function CreatePost(post) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            posts.push(post);
            const error = false;

            if (!error) {
                resolve();
            } else {
                reject('Error Something Went wrong')
            }
        }, 2000)
    })
}
在这里,我创建了另一个等待时间很长的帖子

function CreateAnotherPost(post) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            posts.push(post);
            const error = false;
            if (!error) {
                resolve(console.log(posts));
            } else {
                reject('something went wrong')
            }
        }, 5000);
    })
}

我可以用链锁使它平稳运行。然后。但是我不知道如何使用promise.all

promise.all接受一个承诺数组,等待数组中的所有承诺解析并返回已解析的promise,如果数组中的任何一个承诺中出现任何错误,它将到达catch block

你可以试试这种方法

CreateAnotherPost({
    title: 'Post Five',
    body: 'This is post Five'
}).then(CreatePost({
    title: 'Post Four',
    body: 'This is post Four'
})).then(getPosts).catch(error => console.log(error));

all接受一个承诺数组,等待数组中的所有承诺解析并返回已解析的promoise,如果数组中的任何一个承诺中出现任何错误,它将到达catch块

你可以试试这种方法

CreateAnotherPost({
    title: 'Post Five',
    body: 'This is post Five'
}).then(CreatePost({
    title: 'Post Four',
    body: 'This is post Four'
})).then(getPosts).catch(error => console.log(error));
当您有多个Promise并且希望并行运行这些异步操作时,可以使用Promise.all。要使用它,您只需传递承诺。所有承诺包括:

Promise.all([CreateAnotherPost({
    title: 'Post Five',
    body: 'This is post Five'
  }),
  CreatePost({
    title: 'Post Four',
    body: 'This is post Four'
  })
]).then(getPosts).catch(error => console.log(error));
然后在一个数组中提供结果,该数组根据输入承诺数组进行排序。结果来自承诺,该承诺使用一个值进行解析,而您的值由于某种未知的原因没有解析,这就是您应该如何传递异步结果。如果任何承诺被拒绝,则Promise.all将被拒绝,并且您将只得到被拒绝的原因,而不会得到任何其他结果。

当您有多个承诺并且希望并行运行这些异步操作时,您将使用Promise.all。要使用它,您只需传递承诺。所有承诺包括:

Promise.all([CreateAnotherPost({
    title: 'Post Five',
    body: 'This is post Five'
  }),
  CreatePost({
    title: 'Post Four',
    body: 'This is post Four'
  })
]).then(getPosts).catch(error => console.log(error));
然后在一个数组中提供结果,该数组根据输入承诺数组进行排序。结果来自承诺,该承诺使用一个值进行解析,而您的值由于某种未知的原因没有解析,这就是您应该如何传递异步结果。如果任何承诺被拒绝,则Promise.all将被拒绝,您将只获得被拒绝的原因,而不会得到任何其他结果。

Promise.all接受一系列承诺,并按顺序同步执行, 如果第一个承诺得到解决,那么它将执行第二个承诺。 如果有任何承诺被拒绝,那么它将转到。捕获承诺块。全部

如果所有承诺都成功解析,则它将转到.then PROMITE.all块

希望这有帮助

Promise.all接受一系列承诺,并按顺序同步执行, 如果第一个承诺得到解决,那么它将执行第二个承诺。 如果有任何承诺被拒绝,那么它将转到。捕获承诺块。全部

如果所有承诺都成功解析,则它将转到.then PROMITE.all块


希望这有帮助

@Khant Min Si Thu,如果这种方法解决了您的问题,请告诉我issue@chans泰兄弟。你能帮我吗alot@KhantMinSiThu也接受上述答案,,Thanks@KhantMin Si Thu,让我知道,如果这种方法解决了您的问题issue@chans泰兄弟。你能帮我吗alot@KhantMinSiThu也接受上面的答案,谢谢这是什么。CreateAnotherPost来自哪里?这不在OP的代码中。事实上,这是当你做基于类的实现时,我习惯使用这个。你在这里不需要这个。这个.CreateAnotherPost中的这个是从哪里来的?这不在OP的代码中。事实上,这是当你做基于类的实现时,我习惯使用这个。您在这里不需要这些。@khantminsithu-这些答案中有任何一个为您的问题提供了答案吗?@khantminsithu-这些答案中有任何一个为您的问题提供了答案吗?
Promise.all([
    this.CreateAnotherPost({title: 'Post Five',body: 'This is post Five'}),
    this.CreatePost({title: 'Post Four',body: 'This is post Four'})
   ])
   .then((res)=>{
       // todo after all promise resolves successfully
   })
   .catch((err)=>{
       // todo after if any of the promises resolves rejects
   })