同步執行非同步 NPM 方法 w 回撥
此示例將非同步方法 oauth2.client.getToken(callback)
從包 NPM 包 simple-oauth2
in 包裝到 Fiber 中,以便可以同步呼叫該方法。
const oauth2 = require('simple-oauth2')(credentials);
const credentials = {
clientID: '#####',
clientSecret: '#####',
site: "API Endpoint Here."
};
Meteor.startup(() => {
let token = Meteor.wrapAsync(oauth2.client.getToken)({});
if (token) {
let headers = {
'Content-Type': "application/json",
'Authorization': `Bearer ${token.access_token}`
}
// Make use of requested OAuth2 Token Here (Meteor HTTP.get).
}
});