I'm slightly new to promises so please excuse my lack of knowledge. The code below highlights what I want to do but clearly I am unable to.
var token = "";
var id = "";
var cms = {
login: function () {
return request({
"method": "POST",
"uri": process.env.CMS_URL + "/cms/login",
"json": true,
"body": {
"email": "xxxxxxx",
"password": "xxxxx"
}
}).then(response => {
console.log("Successfully logged in to cms. Access token: " + response);
token = response;
}).catch(error => {
console.log("Couldn't get access token from cms");
});
},
createCollection: function (token) {
return request({
"method": "POST",
"uri": process.env.CMS_URL + "/cms/collection",
"json": true,
"headers": {
"X-Cms-Token": token
},
"body": {
"name": "Acceptance test collection",
"type": "manual"
}
}).then(response => {
console.log("Successfully created a collection.");
id = response.id;
}).catch(error => {
console.log("Collection already exists");
});
},
deleteCollection: function (token, id) {
return request({
"method": "DELETE",
"uri": process.env.CMS_URL + "/cms/collection" + id,
"json": true,
"headers": {
"X-Cms-Token": token
}
}).then(response => {
console.log("Successfully deleted collection.");
}).catch(error => {
console.log("No collection to delete");
console.log(error);
});
}
};
var createCollectionCms = function (token, id) {
return new Promise((resolve) => {
cms.login()
.then(zebedee.createCollection(token))
.then(zebedee.deleteCollection(token, id));
setTimeout(resolve, 6000);
});
}
createCollectionCms();
I need to run each function and pass in "token" and "id". By doing it the way above each function runs at the same time. I need them to execute after each other but pass down the required variables.
Essentially I need to log in, create a "collection", the delete the "collection". This is part of some tests i'm setting up.