栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从Promise返回值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从Promise返回值

如果您依赖承诺来返回数据,则必须从函数中返回承诺。

一旦调用堆栈中的1个函数异步,那么要继续线性执行,所有要调用它的函数也必须异步。(异步=返回承诺)

请注意,您的

if
语句没有花括号,因此,如果条件失败,则仅不会执行后面的第一条语句。

我在此示例中对其进行了修复。请注意我添加的备注。

if(address){    promise=Q($.ajax({        type: "GET",        url: "https://maps.googleapis.com/maps/api/geopre/json?address=" + address + "&key=API_KEY"    }));    return promise.then(function(data) {        // whatever you return here will also become the resolve value of the promise returned by makeGeoCodingRequest        // If you don't want to validate the data, you can in fact just return the promise variable directly        // you probably want to return a rejected promise here if status is not what you expected        if (data.status === "OK") return data; else console.error(messages[data.status]);        return null;        });}

您必须

makeGeoCodingRequest
以以下方式致电。

makeGeoCodingRequest(address,bounds).then(function(data){    // this will contain whatever     console.log(data);});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/469250.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号