2016-09-20 81 views
1

我有這個功能有承諾的鏈條,我想返回一個對象:如何保證承諾的鏈返回一個對象

var signIn = function(fb_ID,fb_accessToken) { 
console.log("signin!") 
var promise = new Parse.Promise(); 
var TokenStorage = Parse.Object.extend("tokenStorage"); 
var query = new Parse.Query(TokenStorage); 
query.equalTo('facebookID', fb_ID); 
query.ascending('createdAt'); 
var password; 
var username; 
var output = {}; 
var user; 
// Check if this ID has previously logged in, using the master key 
return query.first({ useMasterKey: true }).then(function(tokenData) { 

    // Update the accessToken if it is different. 
    if (fb_accessToken !== tokenData.get('fb_accessToken')) { 
     console.log('1') 
     user = tokenData.get('user'); 
     user.fetch({ 
      success: function(data) { 
       username = data.get('username') 
       tokenData.set('fb_accessToken', fb_accessToken); 
       /* 
       password = new Buffer(24); 
       _.times(24, function(i) { 
       password.set(i, _.random(0, 255)); 
       password = password.toString('base64') 
       }) 
       */ 
       password = (Date.now().toString(36) + Math.random().toString(36).substr(2, 10)).toUpperCase(); 

       user.setPassword(password); 
       tokenData.set('password',password) 
       console.log('fetch') 
       return 
      }   

     }).then(function(){ 
      tokenData.save(null, { useMasterKey: true }).then(function(tokenuser) { 
       console.log('tokensave') 
       user.save(); 
       return Parse.User.logIn(username, password); 
      }).then(function(data) { 
       // Return the user object. 
       console.log('output return') 
       output.success = true 
       output.isnewuser = false 
       output.username = username; 
       output.password = password; 
       output.fb_accessToken = fb_accessToken; 
       //return Parse.Promise.as(output); 
       //promise.resolve(output) 
       //return promise 
       return output 
      }); 
     }) 

    } else { 
     console.log('2') 
     Parse.Promise.as().then(function() { 
      username = tokenData.get('username'); 
      password = tokenData.get('password'); 
      return 
     }).then(function(){ 
      return Parse.User.logIn(username, password) 
      .then(function(data) { 
       // Return the user object 
       output.success = true 
       output.isnewuser = false 
       output.username = username; 
       output.password = password; 
       output.fb_accessToken = fb_accessToken; 
       console.log('oo'+JSON.stringify(output)) 
       //return Parse.Promise.as(output); 
       //promise.resolve(output) 
       return output 

      }); 
     }) 

    } 
}) 
//return promise 
} 

該函數有兩個if語句,我想這兩個當我打電話時將返回「輸出」對象:

signIn(fb_ID,fb_accessToken).then(function(data){ 
        if (data.success == true) { 
        console.log('inn'+JSON.stringify(data)) 
        response.success(data) 
        } 
        else { 
        console.log('errr') 
        response.error(data) 
        } 
       }) 

'data'對象應該是SignIn函數中的'output'對象。任何想法?

+0

好吧,既然你已經向我們解釋,你想要做什麼,我會建議做它作爲下一個步驟。 – cdonat

回答

0

您應該在user.fetchtokenData.save之前將return放在第一個if區塊中。在else區塊放Parse.promise.as之前return

您目前的實施看起來像這樣的東西。

var someFunction = function() { 
     return mainPromise().then(function() { 
      if (someCondition) { 
       somePromiseOne().then(function() { 
        return data; 
       }); 
      } 
      else { 
       somePromiseTwo().then(function() { 
        return data; 
       }); 
      } 
     }); 
    }; 

在這兩種情況下,你應該把returnsomePromiseOne和之前爲了給data傳遞給許鏈。

0

你可以做很多工作來整理東西......並讓它工作。

首先,您可以(也應該)將user.fetch({ success:... })回調中的代碼移動到以下回調函數中。當承諾界面可用(並且已經被使用)時,應該避免「Property-style」回調。

然後,你可能旨在用於流控制,如下所示:

doSomethingAsync().then(function() { 
    var promise; 
    if(condition) { 
     promise = ...; // deliver username and password 
    } else { 
     promise = ...; // deliver username and password 
    } 
    return promise.then(function(details) { 
     return login().then(function() { 
      // fully compose output object here 
      return details; 
     }); 
    }); 
}); 

在實踐中,該代碼將更加廣泛由於異步步驟的數目和的同步代碼的體積,尤其是在第一個條件。

在全面,你可能最終得到這樣的:

var signIn = function(fb_ID, fb_accessToken) { 
    var query = new Parse.Query(Parse.Object.extend('tokenStorage')); 
    query.equalTo('facebookID', fb_ID); 
    query.ascending('createdAt'); 
    // Check if this ID has previously logged in, using the master key 
    return query.first({ useMasterKey: true }).then(function(tokenData) { 
     var promise; 
     // Update the accessToken if it is different. 
     if (fb_accessToken !== tokenData.get('fb_accessToken')) { 
      var user = tokenData.get('user'); 
      promise = user.fetch().then(function(data) { 
       var details = { 
        username: data.get('username'), 
        password: (Date.now().toString(36) + Math.random().toString(36).substr(2, 10)).toUpperCase() 
       }; 
       tokenData.set('fb_accessToken', fb_accessToken); 
       tokenData.set('password', details.password); 
       user.setPassword(details.password); 

       // Assuming here that `tokenData.save()` and `user.save()` are both async and can be executed in parallel. 
       return Parse.Promise.when([ 
        tokenData.save(null, { useMasterKey: true }), 
        user.save() 
       ]).then(function() { 
        return details; 
       }); 
      }); 
     } else { 
      // here `promise` mimics the final successful state of the promise in the other condition. 
      // ie. promise-wrapped details 
      promise = Parse.Promise.as({ 
       username: tokenData.get('username'), 
       password: tokenData.get('password') 
      }); 
     } 
     return promise.then(function(details) { 
      // whichever condition was executed, username and password details are delivered here in an identical format. 
      return Parse.User.logIn(details.username, details.password).then(function() { 
       details.success = true; 
       details.isnewuser = false; 
       details.fb_accessToken = fb_accessToken; 
       return details; // deliver details to signIn's caller 
      }); 
     }); 
    }); 
}