美文网首页
async / await

async / await

作者: 小马哒哒哒哒哒哒 | 来源:发表于2019-12-31 16:44 被阅读0次
    
    let time = () => {
    
        return new Promise((resolve,reject) => {
    
            setTimeout(() => {
    
              resolve('{"name":[1,2]}')
    
            },500)
    
        })
    
    }
    
    let fn = async () => {
    
        try{
    
            let objstr = await time();
    
            console.log(JSON.parse(objstr));
    
        }catch(e){
    
            throw new Error('对象字符串的 key 只能用双引号, 外部使用单引号')
    
        }
    
    }
    
    fn();
    
    
    async await promise.png

    相关文章

      网友评论

          本文标题:async / await

          本文链接:https://www.haomeiwen.com/subject/itfioctx.html