The main problem is that your fulfillment handler on the promise from firstMethod
doesn't return anything. If you want it to return the promise from secondMethod
so that the chain waits on that promise, you need to do that explicitly:
firstMethod().
then((data)=>{
console.log("firtMethod data ",JSON.stringify(data));
return secondMethod(data);
// ^^^^^^
}).
then((data)=>{
console.log("second Method data",JSON.stringify(data));
resolve("final data",data);
});
But, there are some other things you should probably fix as well.
First, you haven't declared either firstMethod
or secondMethod
, which means that code is falling prey to what I call The Horror of Implicit Globals. Declare your variables.
Second, you're passing more than one argument to resolve
:
resolve("final data",data);
resolve
ignores all arguments except the first one.
Third, your code is using the promise creation anti-pattern. You already have a promise from firstMethod
, you don't need to create a new promise. Just chain off the one you already have.
Instead of:
return new Promise((resolve, reject)=>{
firstMethod().
then((data)=>{
console.log("firtMethod data ",JSON.stringify(data));
return secondMethod(data);
}).
then((data)=>{
console.log("second Method data",JSON.stringify(data));
resolve("final data",data);
});
});
in a non-async
function you'd use:
return firstMethod().
then((data)=>{
console.log("firtMethod data ",JSON.stringify(data));
return secondMethod(data);
}).
then((data)=>{
console.log("second Method data",JSON.stringify(data));
return "final data");
});
This also has the advantage of ensuring that rejections are passed back to the caller for the caller to handle.
But, since your wrapper function is an async
function, you can use await
instead of explicitly consuming the promises:
exports.handler = async (event) => {
const firstMethod = ()=>{
return new Promise(function(resolve, reject){
setTimeout(function() {
resolve({data: '123'})
}, 2000);
});
};
const secondMethod = (someStuff)=>{
return new Promise(function(resolve, reject){
setTimeout(function() {
resolve({newData:someStuff.data + ' some more data'})
}, 2000);
});
};
const data = await firstMethod();
console.log("firtMethod data ",JSON.stringify(data));
await secondMethod(data);
console.log("second Method data",JSON.stringify(data));
return "final data";
};