Skip to content
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

Closed
fangjj opened this issue Dec 8, 2017 · 12 comments
Closed

how to deal with async and await in register? #16

fangjj opened this issue Dec 8, 2017 · 12 comments
Labels

Comments

@fangjj
Copy link

fangjj commented Dec 8, 2017

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.

@msavin
Copy link
Owner

msavin commented Dec 8, 2017 via email

@fangjj
Copy link
Author

fangjj commented Dec 8, 2017

ok, i try wrapped async make it sync

@msavin
Copy link
Owner

msavin commented Jan 12, 2018

@fangjj how did it work?

@msavin msavin added the bug label Jan 12, 2018
@jpsear
Copy link

jpsear commented Jan 29, 2018

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...

@msavin
Copy link
Owner

msavin commented Jan 30, 2018 via email

@jpsear
Copy link

jpsear commented Jan 30, 2018

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...`)
  }
}

@msavin
Copy link
Owner

msavin commented Jan 30, 2018

@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

@jpsear
Copy link

jpsear commented Jan 31, 2018

@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...

I20180131-13:24:38.103(0)? hitting handleSomething()
I20180131-13:24:38.493(0)? hitting handleSomething()
I20180131-13:24:38.889(0)? hitting handleSomething()
I20180131-13:24:39.308(0)? hitting handleSomething()
I20180131-13:24:39.706(0)? hitting handleSomething()
I20180131-13:24:40.128(0)? hitting handleSomething()
I20180131-13:24:40.536(0)? hitting handleSomething()
I20180131-13:24:40.910(0)? hitting handleSomething()
I20180131-13:24:41.431(0)? hitting handleSomething()
I20180131-13:24:41.683(0)? hitting handleSomething()
I20180131-13:24:42.053(0)? hitting handleSomething()
I20180131-13:24:42.433(0)? hitting handleSomething()
I20180131-13:24:42.740(0)? got a result.
I20180131-13:24:42.827(0)? hitting handleSomething()
I20180131-13:24:43.049(0)? got a result.
I20180131-13:24:43.267(0)? got a result.
I20180131-13:24:43.871(0)? got a result.
I20180131-13:24:44.138(0)? got a result.
I20180131-13:24:44.663(0)? got a result.
I20180131-13:24:45.077(0)? got a result.
I20180131-13:24:45.293(0)? got a result.
I20180131-13:24:45.718(0)? got a result.
I20180131-13:24:46.075(0)? got a result.
I20180131-13:24:46.591(0)? got a result.
I20180131-13:24:46.820(0)? got a result.
I20180131-13:24:47.263(0)? got a result.

Is Jobs polling?

@msavin
Copy link
Owner

msavin commented Jan 31, 2018

@jpsear Is that the complete code? it doesn't look look like you are using the package correctly

@jpsear
Copy link

jpsear commented Jan 31, 2018

The only part I missed was this:

import queue from './queue'

Jobs.register({ queue })

@msavin
Copy link
Owner

msavin commented Feb 8, 2018

@jpsear I'm not so familiar with ES6, but is it possible that this.success is being executed in the wrong context?

@msavin
Copy link
Owner

msavin commented Feb 8, 2018

@jpsear I think that might be it. Since we're getting a bit off-topic, if the issue persists, please open a new ticket :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants