Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $.ajax({
- url: "//localhost:3000/api/tokens",
- type: "POST",
- data: JSON.stringify({
- user: {
- email: 'admin@admin.com',
- password: 'password123'
- }
- }),
- contentType: "application/json"
- })
- .then(data => data.token.encoded) // OK
- .then(token => Farmbot({ token: token })) // OK
- .then(function(bot){ // OK
- return new Promise(function(resolve, reject) {
- bot.connect(function(){ resolve(bot); });
- });
- }, errorr)
- .then(function(bot){ // NOT OK!
- // passes in an unresolved promise object, which is useless.
- //
- bot; // => {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
- });
- // ...
- .then(function(bot){
- return $.Deferred(function(defer) {
- bot.connect(function(){ defer.resolve(bot); });
- });
- }, errorr)
- // ...
- Promise.resolve($.ajax({
- // ...
- }))
- .then(data => data.token.encoded)
- // ...
- var jqPromise = $.ajax({
- url: "//localhost:3000/api/tokens",
- type: "POST",
- data: JSON.stringify({
- user: {
- email: 'admin@admin.com',
- password: 'password123'
- }
- }),
- contentType: "application/json"
- })
- .then(data => data.token.encoded) // OK
- .then(token => Farmbot({ token: token })) // OK
- ;
- var es6Promise = Promise.resolve(jqPromise); // convert to ES6 promise
- es6Promise.then(function(bot){ // OK
- return new Promise(function(resolve, reject) {
- bot.connect(function(){ resolve(bot); });
- });
- }, errorr)
- .then(function(bot){ // will not pass in unfulfilled promise
- // process bot value
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement