NiL.JS icon indicating copy to clipboard operation
NiL.JS copied to clipboard

Promise catch method not working

Open KaivnD opened this issue 3 years ago • 1 comments

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 avatar May 05 '21 11:05 KaivnD

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

KaivnD avatar May 05 '21 11:05 KaivnD