I have to write the body of a function getABC() which essentially has to take in responses from 3 different functions, getA()(synchronous),getB(callback)(callback driven),getC() (promise based). Ultimately I have to return a promise aggregating each function’s result, like [(result of getA),(result of getB),(result of getC)].
I am pasting the snippet of the code down below:
const [A,B,C] = ['A','B','C']; function getA(){ return A; } function getB(cb){ setTimeout(()=>{ cb(B); },10); } function getC() { return Promise.resolve().then(()=>C) } function getABC() {//Can only write the body of this function var ARR=[]; const cb = async x =>{ const arr = []; arr.push(getA()); arr.push(x); arr.push(await getC()); console.log(arr); //Shows ['A','B','C'] required output return arr; } let res = getB(cb); console.log(res); //Shows undefined return Promise.resolve().then(()=>ARR); } let arr = getABC().then((arr)=> console.log('Arr:',arr)); //Shows empty array ARR
` I cannot edit any other part of the code excluding the getABC() function call. I cannot wrap my head around this problem. Any help would be much appreciated. Thanks in advance!
Advertisement
Answer
Like so:
const[A,B,C]=["A","B","C"];function getA(){return A}function getB(t){setTimeout(()=>{t(B)},10)}function getC(){return Promise.resolve().then(()=>C)} function getABC() { return new Promise(async(resolve) => { const a = getA() const c = await getC() getB((b) => resolve([a, b, c])) }) } getABC().then(res => console.log(res))
The result from getA
is returned immediately. getC
returns a promise, so just await
its result. Then call getB
and resolve the promise in the callback.
Another option is to make getABC
an async
function as well. Note the difference in the way getB
is called here.
const[A,B,C]=["A","B","C"]; function getA(){return A} function getB(t){setTimeout(()=>{t(B)},10)} function getC(){return Promise.resolve().then(()=>C)} async function getABC() { const a = getA() const b = await new Promise(resolve => getB(resolve)) const c = await getC() return [a, b, c] } getABC().then(res => console.log(res))
The getB
line can be further simplified using a technique called eta reduction. The same technique can be used for the .then(res => console.log(res))
line.
async function getABC() { const a = getA() const b = await new Promise(getB) // eta reduced const c = await getC() return [a, b, c] } getABC().then(console.log) // eta reduced