1

How can we resolve promise to a normal number value .

I have use case in protractor automation in the first i have to call a asynchronous operation then that result value which should not be a promise .

I am using protractor framework

EDIT

var mobileNumber = database.generateMobileNumber().then(function(mobileNumber){
    done();
    return mobileNumber;
}); 
var number=Promise.resolve(mobileNumber);
David S.
  • 10,578
  • 12
  • 62
  • 104
Vivek
  • 151
  • 2
  • 14

2 Answers2

0

Not quite sure why you might want to work with non-promise values, but i think you should play with browser.wait() I didn't checked this, test this code to see if it will work. This approach is bad, think twice before use it:

function getMobileNumber() {
var result;
var promise = database.generateMobileNumber().then(mobileNumber=> {
    result = mobileNumber;
    return true;
});

browser.wait(promise, 10000)
return result;
}
Xotabu4
  • 3,063
  • 17
  • 29
0

How about this ? You can find in this article more information about managing promises with protractor.

var mobileNumber = database.generateMobileNumber().then(function(mobileNumber){
    done();
    var deferred = protractor.promise.defer();
    return deferred.fulfill(mobileNumber);
});

EDIT

var mobileNumber = database.generateMobileNumber().then(function(value){
    done();
    var deferred = protractor.promise.defer();
    return deferred.fulfill(value);
});

the previous one is not clean as the same name (mobileNumber) is used in two different contexts. I don't know the result of this.

Community
  • 1
  • 1
  • when i try to run it says as Failed: deferred.resolve is not a function – Vivek Apr 06 '16 at 07:04
  • can i know the usage of deferred object in protractor – Vivek Apr 06 '16 at 08:54
  • I can not find any documents or info in webdriver or protractor website about deferred object info and usage – Vivek Apr 06 '16 at 08:55
  • var mobileNumber = database.generateMobileNumber().then(function(mobileNumber){ done(); var deferred = protractor.promise.defer(); return deferred.fulfill(mobileNumber); }); console.log(mobileNumber); – Vivek Apr 06 '16 at 08:57
  • can i know when to use defer object @stephane – Vivek Apr 06 '16 at 09:14
  • OK, please update your comment instead of creating new ones. And please let me some time to answer you. I am not spending all my time on stackoverflow and I want you to respect that. – Stéphane Vercouillie Apr 06 '16 at 10:25
  • The defer() function applies to a promise and tells to protractor that you will manually resolve the promise yourself. With that function, you can create your own promises and resolve it when you want, with the value you want. This might be exactly what you want, because you can resolve a promise with a simple number that way. Now be careful that when you write `myfunction().then(function(myvalue))`, the value of 'myvalue' is the resolved promise returned by `myfunction()`. It is dangerous then to write then(function(mobileNumber)) cause it could lead to unwanted behaviour. – Stéphane Vercouillie Apr 06 '16 at 10:31