I am trying to create a series of REST calls in a synchronous order, and I was referred to this library. So far, I have not been able to get these calls to run in the desired order. I am using the request.js library to make the calls, giving each on it's own function and wrapping all of the functions within a fiber. Based on the code below, I would expect that I would get the numbers 1 - 6 in order via the console.logs, but I keep getting them in a random order. I have tried several different ways of wrapping them in sync.await() functions using the sync.defer(), etc. I am pretty confused by the documentation to be completely honest. Any help would be greatly appreciated.
var sync = require('synchronize');
var request = require('request');
var getTranslations = function() {
var options = {
method: 'GET',
url: 'https://dev.sqor.com/locales/en/translation.json?_=1450202479259',
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
json: true
};
request(options, function(error, response, body) {
if (error){
// context.fail("There was an issue with getTranslations and you need to solve it, buddy!!");
console.log("Oops, an error with getTranslations!");
} else {
console.log("1");
console.log("We know that we got the translations, because our status code was " + response.statusCode);
}
});
};
var getPostPermalink = function() {
var options = {
method: 'GET',
url: 'https://rest-dev.sqor.com/posts/ac080247-0d14-4335-9eb1-b5af6d7aa9e2',
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
'user-id': '0',
'origin': 'https://dev.sqor.com',
'referer': 'https://dev.sqor.com/posts/ac080247-0d14-4335-9eb1-b5af6d7aa9e2'
},
json: true
};
request(options, function(error, response, body) {
if (error) {
console.log("Ooops, an error with getPostPermalink!");
} else {
console.log("2");
console.log("The Permalink is there, and it's id is " + response.body.id);
}
});
};
var likePostAtPermalinkNotSignedIn = function() {
var options = {
method: 'POST',
url: 'https://rest-dev.sqor.com/favorites/ac080247-0d14-4335-9eb1-b5af6d7aa9e2',
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
'user-id': '0',
'origin': 'https://dev.sqor.com',
'referer': 'https://dev.sqor.com/posts/ac080247-0d14-4335-9eb1-b5af6d7aa9e2'
},
json: true
};
request(options, function(error, response, body) {
if (error) {
console.log("You were able to like this post, hence the status code " + response.statusCode);
} else {
console.log("3");
console.log("You can't like this post because you are not logged in, hence the status code of " + response.statusCode);
}
});
};
var loginFromPermalink = function() {
var options = {
method: 'POST',
url: 'https://rest-dev.sqor.com/auth/email/login',
headers: {
'content-type': 'application/json;charset=UTF-8',
'user-id': '0',
'origin': 'https://dev.sqor.com',
'referer': 'https://dev.sqor.com/posts/ac080247-0d14-4335-9eb1-b5af6d7aa9e2'
},
body: {
'email': 'andrewdpohl@gmail.com',
'password': 'Password1!'
},
json: true
};
request(options, function(error, response, body) {
if (error) {
console.log("Oops, there was an issue trying to login from a permalink!");
} else {
console.log("4");
console.log("Successfully logged in from a permalink!! " + response.statusCode);
}
});
};
var likePostAtPermalinkSignedIn = function() {
var options = {
method: 'POST',
url: 'https://rest-dev.sqor.com/favorites/ac080247-0d14-4335-9eb1-b5af6d7aa9e2',
headers: {
'access-token': 'xQ6YRAnVDH0yFPL2knjkXjFca030SY9KHUh9Qj61e9Mx1iSZ4peux9piOpmr3nf8',
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
'user-id': '0',
'origin': 'https://dev.sqor.com',
'referer': 'https://dev.sqor.com/posts/ac080247-0d14-4335-9eb1-b5af6d7aa9e2'
},
json: true
};
request(options, function(error, response, body) {
if (error) {
console.log("You can't like this post becuase you are not logged in, hence the status code of " + response.statusCode);
} else {
console.log("5");
console.log("You CAN like this post because you ARE logged in, hence the status code of " + response.statusCode);
}
});
};
unlikePostAtPermalink = function() {
var options = {
method: 'DELETE',
url: 'https://rest-dev.sqor.com/favorites/ac080247-0d14-4335-9eb1-b5af6d7aa9e2',
headers: {
'access-token': 'xQ6YRAnVDH0yFPL2knjkXjFca030SY9KHUh9Qj61e9Mx1iSZ4peux9piOpmr3nf8',
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
'user-id': '212733',
'origin': 'https://dev.sqor.com',
'referer': 'https://dev.sqor.com/posts/ac080247-0d14-4335-9eb1-b5af6d7aa9e2'
},
json: true
};
request(options, function(error, response, body) {
if (error) {
console.log("There was a problem with unliking a post!");
} else {
console.log("6");
console.log("You were able to unlike that post successfully, bro!!");
}
});
};
sync.fiber(function() {
likePostAtPermalinkNotSignedIn();
loginFromPermalink();
likePostAtPermalinkSignedIn();
unlikePostAtPermalink();
sync.await(getPostPermalink(getTranslations(), sync.defer()));
});
I am trying to create a series of REST calls in a synchronous order, and I was referred to this library. So far, I have not been able to get these calls to run in the desired order. I am using the request.js library to make the calls, giving each on it's own function and wrapping all of the functions within a fiber. Based on the code below, I would expect that I would get the numbers 1 - 6 in order via the console.logs, but I keep getting them in a random order. I have tried several different ways of wrapping them in sync.await() functions using the sync.defer(), etc. I am pretty confused by the documentation to be completely honest. Any help would be greatly appreciated.