Javascript-按顺序调用两个异步函数
我有一个类似这样的代码Javascript-按顺序调用两个异步函数,javascript,asynchronous,promise,Javascript,Asynchronous,Promise,我有一个类似这样的代码 class foo { constructor() { // calling a, b, c synchronously a().then( function(response) { b().then( function(response) { c();
class foo {
constructor() {
// calling a, b, c synchronously
a().then(
function(response) {
b().then(
function(response) {
c();
}, function(error) {
// pass
}
);
}, function(error) {
// pass
}
);
}
a() {
return new Promise(function(fullfill, reject) {
// x is any function that returns a promise, making the
// implementation of this function "a" asynchronous
x().then(
function(response) {
fullfill(response);
}, function(error) {
reject(error);
}
);
});
}
b() {
return new Promise(function(fullfill, reject) {
// x is any function that returns a promise, making the
// implementation of this function "b" asynchronous
x().then(
function(response) {
fullfill(response);
}, function(error) {
reject(error);
}
);
});
}
c() {
// do something
}
}
基本上,我有两个函数a和b,它们都是异步的。
这些函数是异步的,因为它们都调用某个函数x,该函数返回一个承诺(在我的例子中,它是对数据库的查询)
我需要依次调用a,b,c。一种方法是我在上面的代码中实现的,但它会导致令人讨厌的嵌套承诺响应
有没有其他方法可以实现相同的结果,而无需使用以下语法,因为如果这是唯一可能的解决方案,那么我最好根本不使用它。使用承诺链接,如下所示:
a().then(resA => {
// resA is response of function a()
// Do whatever you want with resA here
// and then return promise of function b()
return b();
})
.then(resB => {
// resB is response of function b()
// Do whatever you want with resA here
// and then return promise of function c()
return c();
})
.then(resC => {
// resC is response of function c()
// Do whatever you want with resC here
})
.catch(err => {
// Handle any reject/error of functions a, b or c
});
如果您想将函数a和b的响应进行到底,则可以按如下方式进行链接:
a().then(resA => {
// resA is response of function a()
// Do whatever you want with resA here
// and then return promise of function b()
return b().then(resB => { resA: resA, resB: resB });
})
.then(res => {
// res is a object with rsponse of function a() & b()
// res.resA is response of function a()
// res.resB is response of function b()
// Do whatever you want to with the responses here
// then return promise of function c()
return c().then(resC, { resA: res.resA, resB: res.resB, resC: resC });
})
.then(resC => {
// res is a object with rsponse of function a(), b() & c()
// res.resA is response of function a()
// res.resB is response of function b()
// res.resC is response of function c()
// Do whatever you want to with the responses
})
.catch(err => {
// Handle any reject/error of functions a, b or c
});
以上代码是为了演示/解释而精心编写的。如下图所示,可以使用ES6减少
a().then(resA => {
// resA is response of function a()
// Do whatever you want with resA here
// and then return promise of function b()
return b().then(resB => { resA, resB });
})
.then(res => {
// res is a object with rsponse of function a() & b()
// res.resA is response of function a()
// res.resB is response of function b()
// Do whatever you want to with the responses here
// then return promise of function c()
return c().then(resC, Object,assign(res, { resC }));
})
.then(resC => {
// res is a object with rsponse of function a(), b() & c()
// res.resA is response of function a()
// res.resB is response of function b()
// res.resC is response of function c()
// Do whatever you want to with the responses
})
.catch(err => {
// Handle any reject/error of functions a, b or c
});
希望这有帮助。让seq=[f1,f2,…,fn]成为一个零参数序列 返回函数。建立一个承诺p来执行它们 按顺序在f{i-1}之后调用fi var p=顺序减少 p_acc,f=>p_acc.then=>f 那你可能想做什么
p、 捕获…使用承诺链接@atmd Yes。a、 b和c是我写的函数。但是它们都对数据库进行查询,并且这些查询是异步的。我无法控制这些查询。@m14所说的,只要做a.thenb.thenc;。如果x有一个。那么它已经是一个承诺了。在这种情况下,不要自己创建承诺,只需执行{return x}。这就是全部。只有在x没有返回承诺的情况下,你才没有承诺。那么你应该自己创建一个承诺。顺便说一句,同步!==按顺序。不能同步异步功能;但是您可以按顺序调用几个异步函数…