简体   繁体   English

Javascript:Promise中的.then()未执行?

[英]Javascript: .then() in promise is not executing?

I want a script file just for accessing a JSON from a url. 我想要一个仅用于从URL访问JSON的脚本文件。 I want to have a method to return the JSON object from the script into a var in a separate script file then modify it accordingly. 我想要一种方法,可以将脚本中的JSON对象返回到单独脚本文件中的var中,然后相应地对其进行修改。 Obviously since javascript is asynchronous it fails if I simply write it out. 显然,由于javascript是异步的,因此如果我简单地将其写出来,它将失败。 Therefore I must use a promise: 因此,我必须承诺:

var promise1 = new Promise(function(resolve, reject) {
  access(userSearchText); //send user input to get appropriate JSON object
});

promise1.then(function(value) {
  var getData = returnData();     //method that returns JSON object from the other script
  alert(getData);                 //print out the returned JSON object
  console.log("we have finished");
});

But this is not printing out anything. 但这并没有打印出任何东西。 The alert does not occur at all and nothing is being printed in the console. 警报根本不会发生,并且控制台中未打印任何内容。 I am using jquery to access the url in the other script and I have a .done after accessing it in the script for retrieving the JSON as: access.js 我正在使用jquery访问其他脚本中的url,在脚本中访问它以获取JSON的方式后有一个.done:access.js

var sendData;

(function() {

        jqxhr = $.getJSON( url, {

        })
        .done(function( data ) {
            sendData = data;

    });
})();

function returnData(){
    return sendData;
}

So there should not be any asynchronous issues in the script for accessing the URL because of .done. 因此,由于.done,脚本中不应存在任何用于访问URL的异步问题。 I really don't understand why this isn't working. 我真的不明白为什么这不起作用。 I inspected my page and there are no errors to be seen anywhere. 我检查了我的页面,发现任何地方都没有错误。 Can anyone help? 有人可以帮忙吗?

promise1 never gets resolved. promise1永远不会解决。

In your definition of promise1 , you never call the resolve() function to resolve it. promise1的定义中,您永远不会调用resolve()函数来解决它。 Thus, it's .then() never gets called. 因此,它永远不会被调用.then()

Manually created promises are only resolved when you explicitly call resolve() in the executor function. 手动创建的承诺仅在您在执行程序函数中显式调用resolve()时才能解决。

If access() is itself asynchronous, then we will need to more about that function to help you do this correct. 如果access()本身是异步的,那么我们将需要更多有关该函数的信息,以帮助您正确地完成此操作。 Wrapping a promise around it does not do anything to help you. 兑现承诺并不能帮助您。 If access() itself returns a promise, you should just use that promise and not wrap one around it. 如果access()本身返回一个承诺,则应仅使用该承诺,而不能在其周围包裹一个承诺。


Probably access() should return a promise that is resolved when it's asynchronous operation is done and then you can do: 可能access()应该返回一个承诺,该承诺在完成异步操作后便会解决,然后您可以执行以下操作:

access().then(...).catch(...);

And, not wrap it with another promise. 而且,不要用其他承诺来包装它。

The sendData and returnData() stuff just looks wrong. sendDatasendData returnData()东西看起来只是错误。 You'd have to explain and show more context to know what exactly the recommend. 您必须解释并显示更多背景信息才能确切了解建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM