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

Embedded Promise not Resolving #140

Open
Matchlighter opened this issue Dec 4, 2020 · 6 comments
Open

Embedded Promise not Resolving #140

Matchlighter opened this issue Dec 4, 2020 · 6 comments

Comments

@Matchlighter
Copy link

Matchlighter commented Dec 4, 2020

I've been building a CLI tool and have needed to make use an async library within synchronous functions. I've encountered an issue where deasync will lock up when the result is dependent on a Promise resolving. It works fine with Node 10, but not with 12 or 14. I've managed to come up with this minimal reproducing code:

console.log(0)
function asyncfunc(cb) {
    console.log(1)
    new Promise((resolve) => {
        console.log(2);
        resolve(4)
    }).then((v) => {
        console.log(4)
        cb();
    })
    console.log(3);
}
deasync(asyncfunc)()
console.log(5)

// On Node 12 and 14, prints:
// > 0
// > 1
// > 2
// > 3

I originally discovered this using https://github.com/Yukaii/synchronized-promise/blob/master/lib/index.js, which has some timeout logic. When using that the output is:

> 0
> 1
> 2
> 3
(frozen until timeout)
> 4
> Error:  called timeout

Possibly duplicate of #136 and/or #138.

@victorreinor
Copy link

I'm having the same problem, is there any way to solve this or is there a fix already?

@acarl005
Copy link

Same issue. I have to call synchronous code inside a handler for a webserver which is asynchronous. The synchronous part needs to call an external service where the client library is async-only. I tried using deasync on that client library, and ran into this issue.

@vbgm
Copy link

vbgm commented Feb 18, 2023

+1

1 similar comment
@tiitremmel
Copy link

+1

@jardicc
Copy link

jardicc commented Mar 15, 2023

Avoid this library if there is any good chance to do so. Or if you are not 100% sure how it works. Otherwise, you can easily get your code in deadlock.

@jonathan-annett
Copy link

try this

const deasync = require('deasync');

deasync.promise = function(fn) {
	return function () {
		var done = false
		var args = Array.prototype.slice.apply(arguments)
		var err
		var res

        fn.apply(this, args).then(resolve).catch(reject);

		deasync.loopWhile(function () {
			return !done
		})
		if (err)
			throw err

		return res
		function resolve(r) {
			res = r
			done = true
		}

		function reject(e) {
			err = e
			done = true
		}
	}
};

const readFilePromiseSync = deasync.promise(require('fs').promises.readFile);
const buffer = readFilePromiseSync(__filename);
console.log({buffer});

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

No branches or pull requests

7 participants