New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to deal with async and await in register? #16
Comments
Hmm - you know I'm not sure how this would work with the new JS features.
It's really intended to be used with Fibers. The job waits for something to
be returned, then it moves on to do the next one.
…On Fri, Dec 8, 2017 at 9:04 AM, redim ***@***.***> wrote:
Jobs.register({
rest: async function(url){
const res = await fetch(url, {
method: 'POST'
});
return res;
}
});
i just want fetch a restful api result in job, how can i return the result
in job.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#16>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AB7-g1KG5nDCRp66GzvRwV01XUTkKdIgks5s-O2IgaJpZM4Q6vBt>
.
|
ok, i try wrapped async make it sync |
@fangjj how did it work? |
Any further thoughts on this? It's a feature I'm sorely missing at the mo. I've tried wrapping my async calls in |
The new package requires `this.success()` or `this.failure()` to resolve
the job and start the next one. Can you try it out?
…On Mon, Jan 29, 2018 at 11:54 PM, James Sear ***@***.***> wrote:
Any further thoughts on this? It's a feature I'm sorely missing at the mo.
I've tried wrapping my async calls in Meteor.bindEnvironment with no
luck...
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#16 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AB7-gz6l1x4_9zOjqpd8O0OsRZQgf73rks5tPkxBgaJpZM4Q6vBt>
.
|
Yup, that actually worked okay for me. I'll post my code to help anyone that needs a working solution... import handleWebhook from '@integrations/server/webhooks/handleWebhook'
const queueHandleWebhook = async function ({ token, provider, body }) {
try {
const result = await handleWebhook(token, provider, body)
if (result) {
console.info(`Webhook with token: ${token} from provider: ${provider}, successfully run from queue!`)
console.info(result)
this.success()
return
}
} catch (error) {
console.info(`Webhook with token: ${token} from provider: ${provider}, failed from queue`)
console.warn(error)
this.failure()
}
} However, it would be cool if the queue also accepted a Promise too — executing on a resolve and failing on a reject. Then I could write... const queueHandleWebhook = async function ({ token, provider, body }) {
try {
return await handleWebhook(token, provider, body)
} catch (error) {
console.warn(`Failed to run webhook...`)
}
} |
@jpsear if you can PR it, that would be great By the way, you can pass the error and result into this.failure() and this.sucess() - it will save the data into the documents history field |
@msavin I'll give the PR a shot at some point... However, coming back to this today — I don't think it does work as expected. Given the below (I switched to a promise chain, as I think it's more readable here): const queue = function () {
handleSomething()
.then(result => {
console.log('got a result')
this.success(result)
})
.catch(error => {
console.log('got a error')
this.failure(error)
})
}
const handleSomething = () => {
return new Promise((resolve, reject) => {
fetch('https://reqres.in/api/users?page=2')
.then(res => res.json())
.then(json => {
setTimeout(() => {
resolve('result from queue', json)
}, 4000)
})
.catch(error => {
reject('error from queue', error)
})
})
}
Jobs.run('queue') My console actually outputs...
Is Jobs polling? |
@jpsear Is that the complete code? it doesn't look look like you are using the package correctly |
The only part I missed was this: import queue from './queue'
Jobs.register({ queue }) |
@jpsear I'm not so familiar with ES6, but is it possible that |
@jpsear I think that might be it. Since we're getting a bit off-topic, if the issue persists, please open a new ticket :) |
i just want fetch a restful api result in job, how can i return the result in job.
The text was updated successfully, but these errors were encountered: