kahmali / meteor-restivus

REST APIs for the Best of Us! - A Meteor 0.9+ package for building REST APIs https://atmospherejs.com/nimble/restivus
MIT License
544 stars 116 forks source link

Async/await doesn't work. #208

Open AlexFrazer opened 8 years ago

AlexFrazer commented 8 years ago

I want to have a route like this:

Endpoints.addRoute('report', {
  async get() {
    try {
      return await Orders.aggregateAsync(/**...*/);
    } catch (err) {
      return {
        status: 'fail'
      }
    }
  }
});

Just immediately returns the status of the promise.

Naramsim commented 8 years ago

It seems that even if you put a try block Restivus returns immediately

juliedavila commented 7 years ago

Having this challenge as well, tried with Meteor.wrapAsync too

pacozaa commented 7 years ago

Any example how to use Meteor.wrapAsync with restivus?

alexgzhou commented 7 years ago

Need this feature too!

aymericbouzy commented 6 years ago

I'm using meteorhacks:async to turn async code into synchronous code.

I've written a small function on top of it that transforms a promise into synchronous code :

// SyncPromise.js

const { Async } = global

export default promise => {
  const { result, error } = Async.runSync(done =>
    Promise.resolve(promise).then(
      success => done(null, success),
      error => {
        done(error)
      }
    )
  )
  if (error) {
    throw error
  } else {
    return result
  }
}

Then you can use as such :

import SyncPromise from "path/to/SyncPromise"

async function action() {
  // return await stuffYouWantToReturnAsynchronously()
}

Api.addRoute(
  "route",
  {},
  {
    get: {
      action() {
        return SyncPromise(action())
      },
    },
  }
)