locked
Whats the difference between Promise.wrap and Promise.as

    Question

  • Seems to do the same according to the MSDN.
    Thursday, December 13, 2012 6:56 PM

Answers

  • WinJS.Promise.wrap is an older API that's considered deprecated in favor of WinJS.Promise.as. The behavior of wrap is odd if you give it a promise (it wraps a promise in a promise, where the wrapper is fulfilled when the other is fulfilled, but the wrapper's results are undefined. This isn't particularly useful, hence the behavior of as to return the promise if a promise is given, so that the fulfillment behavior is more like you'd expect.

    The guidance I've been told is to always use as if you need to take an arbitrary value and make it into a promise, and just avoid wrap.

    • Marked as answer by phil_ke Sunday, April 28, 2013 4:39 PM
    Sunday, April 28, 2013 4:20 AM

All replies

    • WinJS.Promise.wrap(value): it creates a completed promise with the result: value.
    • WinJS.Promise.as(value): it checks if the value is promise and then returns it. Otherwise it’s same as WinJS.Promise.wrap(value).
    Sunday, April 28, 2013 3:00 AM
  • WinJS.Promise.wrap is an older API that's considered deprecated in favor of WinJS.Promise.as. The behavior of wrap is odd if you give it a promise (it wraps a promise in a promise, where the wrapper is fulfilled when the other is fulfilled, but the wrapper's results are undefined. This isn't particularly useful, hence the behavior of as to return the promise if a promise is given, so that the fulfillment behavior is more like you'd expect.

    The guidance I've been told is to always use as if you need to take an arbitrary value and make it into a promise, and just avoid wrap.

    • Marked as answer by phil_ke Sunday, April 28, 2013 4:39 PM
    Sunday, April 28, 2013 4:20 AM