constfoo=str=>{constlen=str.lengthsetTimeout(()=>{bar({str,len})},len*Math.random())}constbarz=string=>newPromise(resolve=>{//代碼開始foo(string)global.bar=//該怎么寫//代碼結(jié)束})barz('12345').then(o=>console.log(o.len))//5barz('123').then(o=>console.log(o.len))//3barz('123456').then(o=>console.log(o.len))//6已知foo方法,輸入字符串,一段時間后會調(diào)用bar方法現(xiàn)在要寫一個barz方法,把foo封裝成一個promise對應(yīng)的輸入有對應(yīng)的返回,但是代碼該如何寫呢?如何用promise的方式正確handle對應(yīng)的bar?
急求!?。。s promise 封裝異步方法
慕妹3242003
2019-08-17 10:36:22