AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
async 内部是一个迭代器函数,await就像里面的 yield,每次都会执行await后面的函数,并且进行next操作,然后将返回值作为next的参数 ```js function async(generator) { let iterator = generator(); // 获取迭代器 function handle(iteratorResult) { if (iteratorResult.done) return; // 如果迭代结束 const interatorValue = iteratorResult.value; // axios 的返回值 promise console.log(interatorValue, 11); // 如果是个 promise if (interatorValue instanceof Promise) { interatorValue .then((res) => { // res里是Promise.then的数据 handle(iterator.next(res)); // 已经获取到第一次请求的值了,进行的是下一次的迭代 }) .catch((err) => { iterator.throw(err); // 抛出错误 }); } } try { handle(iterator.next()); // 传入 {value: '', done: false} } catch (err) { iterator.throw(err); // 抛出错误 } } async(function* () { try { let res1 = yield axios.get("http://localhost:666"); console.log("res1", res1); let res2 = yield axios.get("http://localhost:666"); console.log("res2", res2); let res3 = yield axios.get("http://localhost:666"); console.log("res3", res3); } catch (err) { console.log("has some error"); } }); ```