0

i'm new to promise in JS and i don't understand why my function don't return the expected result.

Here is my code:

// Extract firstname actor form .docx to JSON

var mammoth = require("mammoth")
var fs = require('fs')

function pdocx (filepath) {
    
    return mammoth.extractRawText({path: filepath})
    .then(function (res) {
        return res.value;
    })
    .then(function (res) {
        let arr = {}
        arr["episode"] = [...new Set(res.match(/(?!Episode #)(\d+)/))]
        arr["names"] = [...new Set(res.match(/^[^:\r\n]+(?=:)/gm))]
        console.log(JSON.stringify(arr, null, '\t'))
        return JSON.stringify(arr, null, '\t')
    })
}

console.log(pdocx("doc.docx"))

The console.log in the function display the good result:

{
"episode": [
    "10600"
],
"names": [
    "Hilary",
    "Jean-Claude",
    "Devon",
    "Jean Claude",
    "Cane",
    "Lily",
    "Jack",
    "Phyllis",
    "Victor",
    "Nikki",
    "Neil",
    "Paul",
    "Dr. Barrett",
    "Christine",
    "Kelly"
]

}

but i have error on the console.log outside the function :

    Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }

Thanks for your help !

EDIT:

It works when i do this, but not sure it's the cleanest way to do promises:

function pdocx (filepath) {
    
    return mammoth.extractRawText({path: filepath})
    .then(function (res) {
        return res.value;
    })
    .then(function (res) {
        let arr = {}
        arr["episode"] = [...new Set(res.match(/(?!Episode #)(\d+)/))]
        arr["names"] = [...new Set(res.match(/^[^:\r\n]+(?=:)/gm))]
        return JSON.stringify(arr, null, '\t')
    })
}

pdocx("doc.docx").then(function (res) {
    console.log(res)
})
amerej
  • 139
  • 1
  • 11

0 Answers0