javascript - Promise isn't resolving on node.js -


i'm having lot of problems node.js/promises @ moment. i'm doing simple this:

module.js

var fs = require('fs');  function myfunction() {     var files = [];      (var = 0; < 100; ++i) {         files.push(fs.writefile("file-" + + ".txt", "file" + i, "utf-8", function(err) {             if (err)                 throw err;         }));     }     promise.all(files).then(function() {         return new promise(function(resolve, reject) {             resolve('ok');         })     }); } exports.myfunction = myfunction; 

main.js

var test = require('./module.js');  test.myfunction().then(function(result) {     console.log(result); }) 

yet, output if run main.js is:

module.myfunction().then(function(result) {                    ^  typeerror: cannot read property 'then' of undefined 

i don't know why module returning promise undefined. can me out here? can't wrap head around this. thank much!

and, while writefile() writes files file-0.txt , on, files have no content @ all.

you code has 2 problems:

  • the native fs.writefile() not return promise
  • your function not return promise

for first problem can either code wrapper or use fs-promise. second problem need function return promise files.

then code this:

var fsp = require('fs-promise');  function myfunction() {   var files = [];    (var = 0; < 100; ++i) {     files.push( fsp.writefile("file-" + + ".txt", "file" + i, "utf-8" ) );   }    return promise.all( files );  } exports.myfunction = myfunction; 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -