我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo?
我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
// return response; // <- I tried that one as well
}
});
return result;
}
var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud) 我有一个返回数据的承诺,我希望将其保存在变量中.这在JavaScript中是不可能的,因为异步性质,我需要onResolve用作回调吗?
我可以以某种方式使用它(例如用async/await包装):
const { foo, bar } = Promise.then(result => result.data, errorHandler);
// rest of script
Run Code Online (Sandbox Code Playgroud)
而不是这个?
Promise.then(result => {
const { foo, bar } = result.data;
// rest of script
}, errorHandler);
Run Code Online (Sandbox Code Playgroud)
注意:使用Bluebird库而不是本机实现,我无法从Promise更改为asnyc/await或Generators.
我在创建一个函数时遇到问题,该函数将停止所有代码直到完成。我想制作异步/等待。在该函数中,我应该进行 fetch,但当我返回结果代码时,它会显示 Promise {}:
const request = async (url) => {
const response = await fetch(url);
const json = await JSON.stringify(response.json());
return json;
}
let tree = request('humans.json');
console.log(tree);Run Code Online (Sandbox Code Playgroud)
如果你知道Promise已经解决了,为什么你不能只是召唤get()它并获得价值?then(..)与使用回调函数相反.
所以不要这样做:
promise.then(function(value) {
// do something with value
});
Run Code Online (Sandbox Code Playgroud)
我希望能够做得更简单:
var value = promise.get();
Run Code Online (Sandbox Code Playgroud)
Java为它的CompletableFuture提供了这个,我认为没有理由不能提供相同的JavaScript.
我有一个 json 文件,我正在尝试读取其内容以在脚本中使用。我使用以下命令来获取 json:
const json = fetch('Data/my_data.json').then(response => response.json());
Run Code Online (Sandbox Code Playgroud)
当我执行此操作并查看控制台中的 json 对象时,我看到它是一个返回的承诺(已解析状态),其中包含文件中的实际 json 对象。但您无法直接访问 PromiseValue。你必须再次使用 then 语句,所以我使用了这个:
json.then(function(getData){metaData = getData;});
Run Code Online (Sandbox Code Playgroud)
metaData 对象未设置为原始 Promise 的值。直到稍后第二个完成时才会取消设置。尽管在最初返回的承诺中该信息已经可用。
是否可以在没有第二个 then 语句的情况下获得原始的承诺值,或者我是否使用第二个 then 语句错误地访问了承诺。
感谢您的任何见解!
我在下面有这个代码:我如何从这个承诺中提取价值?
var value = Auth.loggedinUser().then(function (user){
return user.id
});
console.log(value)
Run Code Online (Sandbox Code Playgroud)
这是我的承诺如下:
Promise {$$state: Object}
$$state:Object
status:1
value:2
__proto__:Object
__proto__:Object
Run Code Online (Sandbox Code Playgroud)
我试图获取值并将其设置为我的值变量.哪个应该是2或者它会返回什么.
我的目标是尽可能将promise的值设置为全局变量
我是 javascript 新手,我正在尝试使用 javascript 读取 JSON 文件,但我不知道如何从承诺结果中访问数据。我在 .json 文件调用 Data.json 中有我的数据,我在承诺中使用 fetch 来加载 JSON 文件并返回结果。如何从承诺结果中获取数据?
JSON 文件中的数据
{
"data": {
"reviewer": [
{
"id": 1,
"name": "susan smith",
"job": "web developer",
"text": "I'm baby meggings twee health goth +1. Bicycle rights tumeric chartreuse before they sold out chambray pop-up. Shaman humblebrag pickled coloring book salvia hoodie, cold-pressed four dollar toast everyday carry"
},
{
"id": 2,
"name": "anna johnson",
"job": "web designer",
"text": "Helvetica artisan kinfolk thundercats lumbersexual blue bottle. …Run Code Online (Sandbox Code Playgroud) 这是我的提取功能:
getAllCountries = async () => {
try {
const response = await fetch("https://restcountries.eu/rest/v2/all");
const result = response.json();
console.log(result);
this.countriesList = result;
} catch (error) {
console.log(error);
}
};
Run Code Online (Sandbox Code Playgroud)
为什么在那里登录了两个 Promise,以及如何访问 PromiseResult。我试过了,console.log(result[0])但没有用
javascript ×8
promise ×4
asynchronous ×2
fetch ×2
ajax ×1
api ×1
async-await ×1
bluebird ×1
ecmascript-6 ×1
es6-promise ×1
fetch-api ×1
jquery ×1