zl程序教程

您现在的位置是:首页 >  后端

当前栏目

async 函数 让方法链式调用 新用法

方法async 函数 用法 调用 链式
2023-09-11 14:19:39 时间

今天发现个async的新用法,之前都是搭配着 promise 让一步函数变得同步,今天试了一下下面的写法

async function hello(){
    return "hello world";
}
hello().then(data=>{
    console.log(data + "哈哈");//hello world哈哈
    return 11;
}).then(a=>{
    console.log(a);//11
}).then(()=>{
    console.log("ssd");//ssd
}).then(()=>{
    console.log("啦啦啦");//啦啦啦
})

这样就是 换一种写法而已,并处理不了setTimeOut这种异步,不过可以让函数变成链式的,看着还不错,记录下来,万一有使用场景了。。。

如果想处理异步 还是 async配合promise使用吧