简体   繁体   English

如何将Promise的值传递给另一个函数?

[英]How can I pass the value of a Promise to another Function?

I'm a little new to Javascript, and am having a hard time with the asynchronous aspect of it. 我对Java有点陌生,但是在异步方面却很难。 My program checks values of two objects, where the second object doesn't have a vital property I need in order to complete the check. 我的程序检查两个对象的值,其中第二个对象没有我需要的重要属性才能完成检查。 So I made a promise to get that value/property (the ID), and now I need to pass that ID value along to a check function. 因此,我承诺要获得该值/属性(ID),现在我需要将该ID值传递给检查函数。 The check function should simply return a true/false to see if the ID's match. 检查函数应仅返回true / false,以查看ID是否匹配。 The value of the check function is passed to another function which then acts appropriately and edits the thing if necessary. 检查函数的值将传递给另一个函数,然后该函数将适当地执行操作,并在必要时进行编辑。 So I basically can't access the value of tick outside it's brackets. 因此,我基本上无法访问方括号之外的tick值。 I've included the snippet of my code where all of this is happening, as all of this is easier to visualize with it. 我已经在所有代码中包含了发生的所有代码片段,因为使用它们可以更轻松地将它们可视化。 Can someone provide me with a solution to this issue? 有人可以为我提供解决方案吗? Any advice would help immensely! 任何建议将极大地帮助您! I want to minimize the modification of the script as much as possible. 我想尽可能地减少对脚本的修改。

    var Q = require('q');

    getID = function(instance, person, callback){
          var = deferred = Q.defer();
          var url = 'www.blah.com'; 
          var options = {
              'url': url
          };

          request.get(options, function(error, response, body){
              if (error) {
                  deferred.reject(error);
              }else{
                  var res = body;
                  var obj = JSON.parse(res);
                  var id = obj.id;
                  deferred.resolve(id);
              } else deferred(obj);
          });


    check = function(instance, thing1, thing2){ 
        var tick = true;
        getID(instance, thing2).then(function(id)){
            var id_1 = thing1.id; // thing1 passed into check with ID
            var id_2 = thing2.id; // thing 2 now has id attached to it
            if( id_1 == id_2 ){
                tick = true; // VALUE 1 
            }else{
                tick = false; // VALUE 2
        });

        // NEED VALUE 1 OR 2 OF TICK HERE 

        if(thing1.name == thing2.name){
            tick = true;
        else{
            tick = false;
        }

        // similar checks to name but with ADDRESS, EMAIL, PHONE NUMBER
        // these properties are already appended to thing1 and thing 2 so no need to call for them

    };

    editThing = function(instance, thing, callback){

        var checked = check(instance, thing1, thing2);
        if(checked){
            // edit thing
        }else{
            // don't edit thing          
    };

Since you're making a promise of work to be done, and you need output from that work, you'll need pass that promise along to the code who's wanting the final output. 由于您承诺要完成的工作,并且需要该工作的输出,因此您需要将该承诺传递给想要最终输出的代码。

I'm not going to try to rewrite the code from your post, so allow me to paraphrase: 我不会尝试重写您的帖子中的代码,因此请允许我解释一下:

getThing = function(thing){
  var deferred = Q.defer();
  ...
  request.get(options, function(error, response, body){
    if (error) {
      deferred.reject(error);
    } else {
      ...
      deferred.resolve(thingMadeFromResponse);
    }
  });
  return deferred;
}

check = function(thingWeHave, thingWeNeedFetched){ 
  return getThing(thingWeNeedFetched).then(function(thingWeFetched)){
    // check logic
    checked = thingWeHave.id == thingWeFetched.id;
    ...
    return checked;
  });
};

editThing = function(instance, thing, callback){
  check(thingWeHave, thingWeNeedFetched).then(function(checked) {
    if(checked){
        // edit thing
    }else{
        // don't edit thing
    }
  });
};

Promises 承诺

“thenable” is an object or function that defines a then method. “ thenable”是定义then方法的对象或函数。

p.then(function(value) {
   // fulfillment
   console.log(value + ' is now available and passable via function argument');
  }, function(reason) {
  // rejection
});

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

相关问题 如何将承诺值传递给函数 - How to pass promise value into a function 如何在另一个函数中使用nodjes许诺值 - How do I use a nodjes promise value in another function 稍后我如何在另一个函数中使用 promise 返回的数据 - How can i use promise returned data in another function later on 如何将价值从功能传递到承诺? - How to pass value from a function to a promise? 如何将xml传递给另一个函数? - How can I pass xml to another function? 如何将值从函数传递到另一个文件中的另一个组件 - How can I pass the value from my function to another component in another file 如何将Apps脚本函数值传递给另一个函数并在那里调用它? - How can I pass an Apps Script function value to another function and have it invoked there? 在JavaScript中,如何将值从未命名函数传递给另一个未命名函数? - In JavaScript, how can I pass value from unnamed function to another unnamed function? 如何在另一个函数的参数中传递 function 的 foreach 的本地值(无事件)? - How can I pass the local value of a foreach of a function in the argument of another function(no events)? 如何将选择值传递给另一个函数,并根据传递的值从Json获取数据? - How can I pass select value to another function and get data from Json according to passed value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM