如何获取js异步请求返回值

使用定时函数模拟异步请求

  • 回调函数

    function doSomething(callback){
        setTimeout(function(){
            console.log('执行结束');
            let result = 4;
            callback(result);
        },100);
    }
    
    function callback(result){
        console.log('接收到结果为:'+result);
    }
    
    doSomething(callback);
    
    //doSomething((result)=>{console.log('接收到结果为:'+result)});
    
  • 使用es6提供的Promise函数

    function doSomething(){
    
        return new Promise(function(resolve){
            setTimeout(function(){
                console.log('执行结束');
                let result = 6;
                resolve(result);
            },100);
        });
    
    }
    
    doSomething().then(result=>{
        console.log('接收到结果为:'+result);
    });
    
  • Generator函数

    function doSomething(){
        setTimeout(function(){
            let result = 6;
            it.next(result);
        },100);
    }
    
    function *gener(){
        var result = yield doSomething();
        console.log(result);
    }
    
    let it = gener();
    it.next();
    
  • 使用es7新特性async

    function doSomething(){
        return new Promise(resolve=>{
            setTimeout(function(){
                let result = 6;
                resolve(result);
            },100);             
        });
    
    }
    
    async function action(){
        let result = await doSomething();
        console.log(result);
    }
    action();
    

你可能感兴趣的:(实用散知识,javascript)