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

Promise catch method not working #243

Open
KaivnD opened this issue May 5, 2021 · 1 comment
Open

Promise catch method not working #243

KaivnD opened this issue May 5, 2021 · 1 comment

Comments

@KaivnD
Copy link

KaivnD commented May 5, 2021

Today I was testing Promise functionality, then is working just fine, but somehow catch callback is not running.
When I go through the source code, I found this line /NiL.JS/BaseLibrary/Promise.cs is throwing reject error, I test my code with debug mode, the line I found in source code is working, but I got nothing in the terminal, so I'm confused, some help please?

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var mainModule = new Module("fakedir/superscript.js", @"
                    import fs from ""fs""
                    fs.readFile(""path/to/file"")
                        .then(res => console.log(res))
                        .catch(err => console.log(err)) // * this line supposed to log file `${path}` is not found
                    ");

                mainModule.ModuleResolversChain.Add(new MyTestModuleResolver());

                mainModule.Run();
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown error: " + e);
            }

            Console.ReadLine();
        }
    }

    public sealed class MyTestModuleResolver : CachedModuleResolverBase
    {
        public override bool TryGetModule(ModuleRequest moduleRequest, out Module result)
        {
            if (moduleRequest.AbsolutePath == "/fakedir/fs.js")
            {
                var module = new Module(moduleRequest.AbsolutePath, @"
                export default {
                    readFile(path, options) {
                        return new Promise((resolve, reject) => {
                            try {
                                if (!io.File.Exists(path)) throw new Error(file `${path}` is not found)
                                const content = io.File.ReadAllText(path)
                                resolve(content)
                            } catch (e) {
                                reject(e)
                            }
                        })
                    }
                }
                ");
                var namespaceProvider = new NamespaceProvider("System.IO");
                module.Context.DefineVariable("io").Assign(namespaceProvider);

                result = module;
                return true;
            }

            result = null;
            return false;
        }
    }
@KaivnD
Copy link
Author

KaivnD commented May 5, 2021

By the way, I'm trying to wrap some native node.js object into my app, and I think Promise is something in Javascript. And finally seems to missing for a Promise object, normaly a Promise object should have finally method.

const p = Promise.resolve();

p.then(() => {
// then stuff 
})
.catch(() => {
// catchstuff 
})
.finally(() => {
// finallystuff 
})

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

1 participant