{"action":"created","issue":{"url":"https://api.github.com/repos/postmanlabs/postman-app-support/issues/5282","repository_url":"https://api.github.com/repos/postmanlabs/postman-app-support","labels_url":"https://api.github.com/repos/postmanlabs/postman-app-support/issues/5282/labels{/name}","comments_url":"https://api.github.com/repos/postmanlabs/postman-app-support/issues/5282/comments","events_url":"https://api.github.com/repos/postmanlabs/postman-app-support/issues/5282/events","html_url":"https://github.com/postmanlabs/postman-app-support/issues/5282","id":365184898,"node_id":"MDU6SXNzdWUzNjUxODQ4OTg=","number":5282,"title":"How to ensure order of two pm.sendRequest calls","user":{"login":"kthulsidoss","id":42998906,"node_id":"MDQ6VXNlcjQyOTk4OTA2","avatar_url":"https://avatars1.githubusercontent.com/u/42998906?v=4","gravatar_id":"","url":"https://api.github.com/users/kthulsidoss","html_url":"https://github.com/kthulsidoss","followers_url":"https://api.github.com/users/kthulsidoss/followers","following_url":"https://api.github.com/users/kthulsidoss/following{/other_user}","gists_url":"https://api.github.com/users/kthulsidoss/gists{/gist_id}","starred_url":"https://api.github.com/users/kthulsidoss/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kthulsidoss/subscriptions","organizations_url":"https://api.github.com/users/kthulsidoss/orgs","repos_url":"https://api.github.com/users/kthulsidoss/repos","events_url":"https://api.github.com/users/kthulsidoss/events{/privacy}","received_events_url":"https://api.github.com/users/kthulsidoss/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-09-30T02:33:24Z","updated_at":"2018-10-03T09:07:37Z","closed_at":"2018-10-01T06:08:34Z","author_association":"NONE","body":"![auth1](https://user-images.githubusercontent.com/42998906/46252519-3e522500-c487-11e8-973e-b872c69f638b.JPG)\r\n\r\nI have 2 requests that needs to be done to set the authentication token , this token is passed to the REST call .\r\nHere is  the code that is there in the pre-tests\r\n--------------------------------------------------------------------------------------------------------------------\r\n```\r\nconst siaPostRequest = {\r\n  url: 'https://iam.stage1.bluemix.net/identity/token',\r\n  method: 'POST',\r\n  header: {\r\n        'Accept': 'application/json',\r\n        'Content-Type': 'application/x-www-form-urlencoded'\r\n      },\r\n      body: {\r\n          mode: 'urlencoded',\r\n          urlencoded: [\r\n            {key: \"grant_type\", value: \"urn:ibm:params:oauth:grant-type:apikey\"},\r\n            {key: \"apikey\", value: \"4YXfPuSZIDhGahHmClJZGFzaGai8xH9ViW4p0XLWvjVr\"},\r\n            {key: \"Accept\", value: \"application/json\"},\r\n            {key: \"Content-Type\", value: \"application/x-www-form-urlencoded\"}\r\n        ]\r\n      }\r\n};\r\n\r\nvar getToken = true;\r\n\r\nif (!pm.environment.get('accessTokenExpiry') || \r\n    !pm.environment.get('currentAccessToken')) {\r\n    console.log('Token or expiry date are missing')\r\n} else if (pm.environment.get('accessTokenExpiry') <= (new Date()).getTime()) {\r\n    console.log('Token is expired')\r\n} else {\r\n    getToken = false;\r\n    console.log('Token and expiry date are all good');\r\n}\r\n\r\nif (getToken === true) {\r\n    pm.sendRequest(siaPostRequest, function (err, res) {\r\n    console.log(err ? err : res.json());\r\n        if (err === null) {\r\n            console.log('***Abt to set the sia access token *******');\r\n            var responseJson = res.json();\r\n            pm.environment.set(\"sia_access_token\",responseJson.access_token);\r\n            console.log('token from env auth1   ' + pm.environment.get(\"sia_access_token\"));\r\n    \r\n            var expiryDate = new Date();\r\n            expiryDate.setSeconds(expiryDate.getSeconds() + 55);\r\n            pm.environment.set('accessTokenExpiry', expiryDate.getTime());\r\n        }\r\n    });\r\n    \r\n    const siaDesiredPostRequest = {\r\n  url: 'https://iam.stage1.bluemix.net/identity/token',\r\n  method: 'POST',\r\n  header: {\r\n        'Accept': 'application/json',\r\n        'Content-Type': 'application/x-www-form-urlencoded'\r\n        \r\n      },\r\n      body: {\r\n          mode: 'urlencoded',\r\n          urlencoded: [\r\n            {key: \"grant_type\", value: \"urn:ibm:params:oauth:grant-type:iam-authz\"},\r\n            {key: \"desired_iam_id\", value: \"crn-crn:v1:bluemix:public:siapoc::a/73be9f58fe97adebbf9fe13eeea2ebe4:instance12345::\"},\r\n            {key: \"Accept\", value: \"application/json\"},\r\n            {key: \"Content-Type\", value: \"application/x-www-form-urlencoded\"},\r\n            {key: \"access_token\", value: pm.environment.get(\"sia_access_token\")},\r\n        ]\r\n      }\r\n  \r\n};\r\n    \r\n  \r\n\r\npm.sendRequest(siaDesiredPostRequest, function (err, res) {\r\n    console.log(err ? err : res.json());\r\n        if (err === null) {\r\n            console.log('******ABt to set the SIA desired token *************************');\r\n            var responseJson1 = res.json();\r\n            pm.environment.set(\"sia_desired_token\",responseJson1.access_token);\r\n            console.log('sia_desired_token' + pm.environment.get(\"sia_desired_token\"));\r\n            \r\n            \r\n        }\r\n    });\r\n\r\n\r\n}\r\n```\r\n-----------------------------------------------------------------------------------------------------------\r\nHere is the response from the log\r\n******ABt to set the SIA desired token *************************\r\n\r\nthen\r\n\r\n\r\n***Abt to set the sia access token *******\r\n******************************************************************\r\n\r\nSo it looks like the order of calling the requests are changed , it need to first then second as the first call output is fed to the second one .So the tests are failing."},"comment":{"url":"https://api.github.com/repos/postmanlabs/postman-app-support/issues/comments/426564322","html_url":"https://github.com/postmanlabs/postman-app-support/issues/5282#issuecomment-426564322","issue_url":"https://api.github.com/repos/postmanlabs/postman-app-support/issues/5282","id":426564322,"node_id":"MDEyOklzc3VlQ29tbWVudDQyNjU2NDMyMg==","user":{"login":"numaanashraf","id":3295568,"node_id":"MDQ6VXNlcjMyOTU1Njg=","avatar_url":"https://avatars3.githubusercontent.com/u/3295568?v=4","gravatar_id":"","url":"https://api.github.com/users/numaanashraf","html_url":"https://github.com/numaanashraf","followers_url":"https://api.github.com/users/numaanashraf/followers","following_url":"https://api.github.com/users/numaanashraf/following{/other_user}","gists_url":"https://api.github.com/users/numaanashraf/gists{/gist_id}","starred_url":"https://api.github.com/users/numaanashraf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/numaanashraf/subscriptions","organizations_url":"https://api.github.com/users/numaanashraf/orgs","repos_url":"https://api.github.com/users/numaanashraf/repos","events_url":"https://api.github.com/users/numaanashraf/events{/privacy}","received_events_url":"https://api.github.com/users/numaanashraf/received_events","type":"User","site_admin":false},"created_at":"2018-10-03T09:07:37Z","updated_at":"2018-10-03T09:07:37Z","author_association":"MEMBER","body":"@kthulsidoss As mentioned by @kamalaknn, `pm.sendRequest` is an asynchronous operation and you need to make the second request in the callback of the first one. This is the standard way to serially call two callback-based async operations in JavaScript.\r\n\r\n```\r\npm.sendRequest(firstRequest, function (err, firstResponse)  {\r\n  // add your extra logic here\r\n  pm.sendRequest(secondRequest, function (err, secondResponse)  {\r\n    // add your extra logic here\r\n  });\r\n});\r\n```"}}
{"actor":{"display_login":"numaanashraf"}}