怎样在小程序里实现标题的更改
573
2022-10-22
jstep是一个使用function*实现异步编程的小函数
jStep
jStep是一个使用function*实现异步编程的小函数。
function* alertDelay(message) { return yield setTimeout(() => { alert(message); this(0, "alertDelay:" + message); }, 1000);}function* asyncTest(time) { var timeDelay = yield setTimeout(() => this(0, 500), time); var message = yield setTimeout(() => this(0, "Hello world"), timeDelay); var alertMessage = yield* alertDelay.call(this, message); console.log(alertMessage);}jStep(null, asyncTest, 500);
异常捕获
jStep((err, data) => { if (err) console.log("return error:" + err.message);}, function* () { try { var timeDelay = yield setTimeout(() => this(0, 500), 500); var message = yield setTimeout(() => this(new Error("Something wrong")), timeDelay); var alertMessage = yield* alertDelay.call(this, message); } catch (err) { console.log("catch:" + err.message); throw err; }});
同步执行等待
jStep(function* () { var waiter = jWait(this); var timeStart = new Date().getTime(); var ret1 = yield setTimeout(function () { this(false, "step1"); }.bind(waiter.step()), 2000); var ret2 = yield setTimeout(function () { this(false, "step2"); }.bind(waiter.step()), 3000); var ret3 = yield setTimeout(function () { this(false, "step3"); }.bind(waiter.step()), 1000); console.log(ret1); //{done:false} console.log(ret2); //{done:false} console.log(ret3); //{done:false} var ret = yield waiter.wait(); console.log(ret); // {error:false,data:[{error:0,data:"step1",done:true}, {error:false,data:"step2",done:true}, {error:false,data:"step3",done:true}]} console.log(ret1); //{error:false,data:"step1",done:true} console.log(ret2); //{error:false,data:"step2",done:true} console.log(ret3); //{error:false,data:"step3",done:true} console.log("执行时间:" + (new Date().getTime() - timeStart)); //大约 3000});
与async/await的对比
jStep
function* alertDelay(message) { return yield setTimeout(() => { alert(message); this(0, "alertDelay:" + message); }, 1000);}function* asyncTest(time) { var timeDelay = yield setTimeout(() => this(0, 500), time); var message = yield setTimeout(() => this(0, "Hello world"), timeDelay); var alertMessage = yield* alertDelay.call(this, message); console.log(alertMessage);}jStep(null, asyncTest, 500);
async/await
async function alertDelay(message) { return await new Promise(s => setTimeout(() => { alert(message); s("alertDelay:" + message); }, 1000));}async function asyncTest(time) { var timeDelay = await new Promise(s => setTimeout(() => s(500), time)); var message = await new Promise(s => setTimeout(() => s("Hello world"), timeDelay)); var alertMessage = await alertDelay(message); console.log(alertMessage);}asyncTest(500);
jPromise
jPromise(function* () { return yield setTimeout(() => this(0, "Hello world"), 1000);}).then(data => alert(data));
jPromise返回的是Promise对象,可以用在任何支持Promise的地方
async function doSomething() { var message = await jPromise(function* () { return yield setTimeout(() => this(0, "Hello world"), 1000); }); alert(message); }doSomething();
npm/require
npm install jstep
var { jStep, jWait, jPromise } = require("jstep");jPromise(function* () { return yield setTimeout(() => this(0, "Hello world"), 1000);}).then(data => console.log(data));
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~