mcneel / rhino3dm

Libraries based on OpenNURBS with a RhinoCommon style
MIT License
609 stars 139 forks source link

I'm I crazy?? #228

Closed ruifortes closed 3 years ago

ruifortes commented 4 years ago

Why the hell can't I wrap this??

var rhino3dm = require('rhino3dm')

function init() {
    return new Promise((resolve, reject) => {
        rhino3dm()
        .then(rhino => {
            // resolve("some other return") this would resolve the promise
            resolve(rhino) // but not this
        })
    })
}

init()
.then(rhino => {
    // never gets here?!
    let sphere = new rhino.Sphere([1,2,3,], 12)
    console.log(sphere.radius)
})
.catch(err => {
    //...thenable returned by rhino doesn't define catch
})

Where is this weirdness coming from? What am I missing here?

Thanks

muka commented 4 years ago

This works for the then but there is no catch and I cannot find a way to detect issues on initialization.

    return new Promise((resolve, reject) => {
      try { rhino3d().then(rhino => resolve({ rhino })) } catch (e) { reject(e) }
    }).then(({ rhino }) => {
      console.log("OK rhino", rhino);
    }).catch((e) => {
      console.error("rhino3dm failure: %s", e)
    });
pearswj commented 3 years ago

This was caused by some weirdness in the rhino3dm.js produced by Emscripten. I just published v0.14.0 to npm which was built using the latest version of Emscripten (#339) – rhino3dm() should now return a proper thenable that can also be awaited.

Thanks @cdriesler for the tip!