在包装在函数中的promise之后调用然后调用

时间:2018-11-02 16:10:33

标签: firebase promise

我将一个带有firebase的promise包装在一个函数中,我想调用它两次,然后在第二次调用完成后执行一些操作。这是我在使用Firebase时易于理解的代码:

    this.updateDB('abc');
    this.updateDB('cde');

    updateDB = (content) => {
     firebase.database().ref('something').update(content)
}

如果仅一次调用数据库更新,就可以使用“ then”。和做某事,但我必须两次调用此函数。两个函数调用完成后,我该怎么办?

我试图研究这个问题变得更加困惑。 不胜感激任何准则

预先感谢

1 个答案:

答案 0 :(得分:0)

由于.update()可以返回承诺,因此您可以将Promise.all()与两个承诺一起使用,以知道它们何时都完成:

Promise.all(this.updateDB('abc'), this.updateDB('cde')).then(() => {
    console.log('both done here');
}).catch(err => {
    console.log('an error in one or both updates occurred');
});


updateDB = (content) => {
    // return promise
    return firebase.database().ref('something').update(content);
}