0

how to get javascript trash the return value? i need to confirm the operation is success or not related to this trash function:

const trash = require('trash');

(async () => {
    await trash(['*.png', '!rainbow.png']);
})();

i've trying this:

(async () => {
    await trash(filename).then( function(val) {
        console.log(val)
    })
})()

[UPDATE]
also this:

trash(filename).then(value => {
    console.log(value)
})

i dont know how to get return value from this function. reference: sindresorhus/trash

from youtube.com i found video about using trash

https://www.youtube.com/watch?v=P2N7mpZ3v1Q

but still not answering my question. the question is updated. please help me.

dhanyn10
  • 704
  • 1
  • 9
  • 26

1 Answers1

0

In case trash promise returns a value, this is how to access it.

// With async await await
(async () => {
    let value = await trash(['*.png', '!rainbow.png']);
    // use value here
})();

// With promise
trash(['*.png', '!rainbow.png']).then(value => {
    // use value here
})

Catching errors

// catching error with async and await
(async () => {
    try {
        await trash(['*.png', '!rainbow.png']);
        // Success
    } catch (ex) {
        // Failed with Error
        console.log('error', ex)
    }
})();

// catching error with Promise
trash(['*.png', '!rainbow.png']).then(() => {
    // Success
}).catch(ex => {
    // Failed with Error
    console.log('error', ex)
})
Wazeed
  • 1,230
  • 1
  • 8
  • 9