一个简单的异步请求,想把他变成同步,大神高手进来看看,就是让a得结果先出来!
function time() {
a();
console.log("love");
}
function a() { setTimeout(function () { console.log(“gun”);
},3000)
}
time();
6 回复
今天刚碰到异步变同步问题感觉好棘手,高手们支援一下!
function sleepSync(time){ return new Promise((resolve,reject)=>{ setTimeout(function(){ resolve(); },time); }) } async bin(){ await sleepSync(500); }
感觉没懂
@DMY-sunny 想异步变同步先搞懂promise,再搞懂es7的async
var a = function () {
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('gun');
resolve();
}, 3000);
});
};
var time = async function () {
await a();
console.log('love');
};
time();