簡體   English   中英

如何將Promise的值傳遞給另一個函數?

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

我對Java有點陌生,但是在異步方面卻很難。 我的程序檢查兩個對象的值,其中第二個對象沒有我需要的重要屬性才能完成檢查。 因此,我承諾要獲得該值/屬性(ID),現在我需要將該ID值傳遞給檢查函數。 檢查函數應僅返回true / false,以查看ID是否匹配。 檢查函數的值將傳遞給另一個函數,然后該函數將適當地執行操作,並在必要時進行編輯。 因此,我基本上無法訪問方括號之外的tick值。 我已經在所有代碼中包含了發生的所有代碼片段,因為使用它們可以更輕松地將它們可視化。 有人可以為我提供解決方案嗎? 任何建議將極大地幫助您! 我想盡可能地減少對腳本的修改。

    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          
    };

由於您承諾要完成的工作,並且需要該工作的輸出,因此您需要將該承諾傳遞給想要最終輸出的代碼。

我不會嘗試重寫您的帖子中的代碼,因此請允許我解釋一下:

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
    }
  });
};

承諾

“ 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM