Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. $.ajax({
  2. url: "//localhost:3000/api/tokens",
  3. type: "POST",
  4. data: JSON.stringify({
  5. user: {
  6. email: 'admin@admin.com',
  7. password: 'password123'
  8. }
  9. }),
  10. contentType: "application/json"
  11. })
  12. .then(data => data.token.encoded) // OK
  13. .then(token => Farmbot({ token: token })) // OK
  14. .then(function(bot){ // OK
  15. return new Promise(function(resolve, reject) {
  16. bot.connect(function(){ resolve(bot); });
  17. });
  18. }, errorr)
  19. .then(function(bot){ // NOT OK!
  20. // passes in an unresolved promise object, which is useless.
  21. //
  22. bot; // => {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
  23. });
  24.  
  25. // ...
  26. .then(function(bot){
  27. return $.Deferred(function(defer) {
  28. bot.connect(function(){ defer.resolve(bot); });
  29. });
  30. }, errorr)
  31. // ...
  32.  
  33. Promise.resolve($.ajax({
  34. // ...
  35. }))
  36. .then(data => data.token.encoded)
  37. // ...
  38.  
  39. var jqPromise = $.ajax({
  40. url: "//localhost:3000/api/tokens",
  41. type: "POST",
  42. data: JSON.stringify({
  43. user: {
  44. email: 'admin@admin.com',
  45. password: 'password123'
  46. }
  47. }),
  48. contentType: "application/json"
  49. })
  50. .then(data => data.token.encoded) // OK
  51. .then(token => Farmbot({ token: token })) // OK
  52.  
  53. ;
  54.  
  55. var es6Promise = Promise.resolve(jqPromise); // convert to ES6 promise
  56.  
  57. es6Promise.then(function(bot){ // OK
  58. return new Promise(function(resolve, reject) {
  59. bot.connect(function(){ resolve(bot); });
  60. });
  61. }, errorr)
  62. .then(function(bot){ // will not pass in unfulfilled promise
  63.  
  64. // process bot value
  65.  
  66. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement