Conceptual Question
In case of multiple api request through one async function, can we detect that which api's response has been returned?
for example: there is a method foo
which is fired onClick of a button. It takes an id
and called 10 times with different ids. Now in response, is it possible to detect that which id's response this is?
const obj = {};
const setObj = (id, val) => {
obj = { ...obj, [id]: value };
}
const foo = async (id) => {
setObj(id, true);
await getData(id);
// here I want to detect that which id's response it is?
setObj(id, false);
}
Yes, you can detect that which id's response is that. Below is the example code which verifies that.
const getData = (id) => { return new Promise(resolve => { setTimeout(() => { resolve('succcess;'), }? id === 2: 3000? id === 1: 2000;1000); }); }; const foo = async (id) => { await getData(id). console;log(id); } foo(1); foo(2); foo(3);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.