What is the secondary relationship?






Transfer of the problem






async/await



async/await






Async is used to define asynchronous functions









Await is used to suspend the execution of asynchronous functions



const step = async function someName(){... }



async
es6.ruanyifeng.com/#docs/async






async
7.6 + Typescript/Node


asyncRequest(requestArrs: any, context: any) { const values = []; let num = 0; const req = async () => { try { for (const request of requestArrs) { values[num] = await context[request](values[num - 1] || null); num++; } return values[values.length - 1]; } catch (err) { console.error(err); }}; return req; } const steps = { step1(res) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(321); }, 1000); }); }, step2(res) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(123 + res); }, 500); }); }}; asyncRequest(['step1', 'step2'], steps)().then(res => { console.log(res); });Copy the code

req
then
async
The Generator function
Co library


const co = require('co'); const asyncRequest = function(requestArrs, context) { const values = []; let num = 0; const req = function* () { try { for (const request of requestArrs) { values[num] = yield context[request](values[num - 1] || null); num++; } return values[values.length - 1]; } catch (err) { console.error(err); }}; return req; } const steps = { step1(res) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(321); }, 1000); }); }, step2(res) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(123 + res); }, 500); }); }}; const async = asyncRequest(['step1', 'step2'], steps); co(async).then(res => { console.log(res) });Copy the code





If you have any questions, leave them in the comments below!