or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

array-utilities.mdconfiguration.mdindex.mdnode-integration.mdobject-utilities.mdpromise.mdutilities.md

node-integration.mddocs/

0

# Node.js Integration

1

2

Convert Node.js callback-style functions to promise-returning functions for better async composition and modern JavaScript patterns.

3

4

## Capabilities

5

6

### denodeify

7

8

Transform Node.js callback-style functions into promise-returning functions.

9

10

```javascript { .api }

11

/**

12

* Convert Node.js callback-style function to promise-returning function

13

* @param nodeFunc - Function following Node callback pattern (err, value)

14

* @param options - Boolean for multiple values or Array for named values

15

* @returns Promisified version of the function

16

*/

17

function denodeify(nodeFunc: Function, options?: boolean | Array): Function;

18

```

19

20

## Basic Usage

21

22

### Simple Function Conversion

23

24

```javascript

25

import { denodeify } from "rsvp";

26

const fs = require('fs');

27

28

// Convert fs.readFile to promise-based

29

const readFile = denodeify(fs.readFile);

30

31

readFile('myfile.txt', 'utf8')

32

.then(function(data) {

33

console.log('File contents:', data);

34

})

35

.catch(function(error) {

36

console.error('Failed to read file:', error);

37

});

38

39

// Original callback version would be:

40

// fs.readFile('myfile.txt', 'utf8', function(err, data) {

41

// if (err) console.error(err);

42

// else console.log(data);

43

// });

44

```

45

46

### Multiple Return Values

47

48

When a Node.js function returns multiple success parameters, use the `options` parameter:

49

50

```javascript

51

const request = require('request');

52

53

// Default - returns only first success parameter

54

const requestSimple = denodeify(request);

55

requestSimple('http://example.com').then(function(response) {

56

// Only gets the response object

57

});

58

59

// With `true` - returns all success parameters as array

60

const requestArray = denodeify(request, true);

61

requestArray('http://example.com').then(function(result) {

62

// result[0] is response object

63

// result[1] is body

64

console.log('Status:', result[0].statusCode);

65

console.log('Body:', result[1]);

66

});

67

68

// With array of names - returns object with named properties

69

const requestNamed = denodeify(request, ['response', 'body']);

70

requestNamed('http://example.com').then(function(result) {

71

console.log('Status:', result.response.statusCode);

72

console.log('Body:', result.body);

73

});

74

```

75

76

## Advanced Usage

77

78

### Preserving `this` Context

79

80

```javascript

81

const express = require('express');

82

const app = express();

83

84

// Bind to preserve `this` context

85

const render = denodeify(app.render.bind(app));

86

87

render('template', { data: 'value' })

88

.then(function(html) {

89

console.log('Rendered HTML:', html);

90

});

91

```

92

93

### Function Inheritance

94

95

The denodified function inherits from the original function, preserving properties:

96

97

```javascript

98

const request = denodeify(require('request'));

99

100

// Original function properties are preserved

101

const cookieJar = request.jar(); // Works because of inheritance

102

103

request('http://example.com', { jar: cookieJar })

104

.then(function(response) {

105

// cookieJar now contains cookies from the response

106

});

107

```

108

109

### Chaining Operations

110

111

```javascript

112

const fs = require('fs');

113

const readFile = denodeify(fs.readFile);

114

const writeFile = denodeify(fs.writeFile);

115

116

// Chain file operations

117

readFile('input.txt', 'utf8')

118

.then(function(data) {

119

const processedData = data.toUpperCase();

120

return writeFile('output.txt', processedData, 'utf8');

121

})

122

.then(function() {

123

console.log('File processing complete');

124

})

125

.catch(function(error) {

126

console.error('File operation failed:', error);

127

});

128

129

// Compare with callback version:

130

// fs.readFile('input.txt', 'utf8', function(err, data) {

131

// if (err) return console.error(err);

132

// const processedData = data.toUpperCase();

133

// fs.writeFile('output.txt', processedData, 'utf8', function(err) {

134

// if (err) return console.error(err);

135

// console.log('File processing complete');

136

// });

137

// });

138

```

139

140

## Promise Input Handling

141

142

denodeify can handle promises as input arguments:

143

144

```javascript

145

const processFile = denodeify(someNodeFunction);

146

147

// Promise arguments are resolved before calling the original function

148

processFile(

149

Promise.resolve('filename.txt'),

150

Promise.resolve('utf8')

151

).then(function(result) {

152

// Both arguments were resolved before calling someNodeFunction

153

});

154

```

155

156

## Error Handling

157

158

```javascript

159

const fs = require('fs');

160

const readFile = denodeify(fs.readFile);

161

162

readFile('nonexistent.txt')

163

.catch(function(error) {

164

if (error.code === 'ENOENT') {

165

console.log('File not found');

166

return 'default content';

167

}

168

throw error; // Re-throw other errors

169

})

170

.then(function(content) {

171

console.log('Content:', content);

172

});

173

```

174

175

## Common Node.js Function Conversions

176

177

### File System Operations

178

179

```javascript

180

const fs = require('fs');

181

182

const readFile = denodeify(fs.readFile);

183

const writeFile = denodeify(fs.writeFile);

184

const stat = denodeify(fs.stat);

185

const readdir = denodeify(fs.readdir);

186

187

// Usage

188

Promise.all([

189

readFile('file1.txt', 'utf8'),

190

readFile('file2.txt', 'utf8'),

191

stat('file3.txt')

192

]).then(function([content1, content2, stats]) {

193

console.log('All operations completed');

194

});

195

```

196

197

### Child Process Operations

198

199

```javascript

200

const { exec } = require('child_process');

201

const execPromise = denodeify(exec);

202

203

execPromise('ls -la')

204

.then(function(result) {

205

console.log('Command output:', result[0]); // stdout

206

console.log('Command errors:', result[1]); // stderr

207

})

208

.catch(function(error) {

209

console.error('Command failed:', error);

210

});

211

```

212

213

### Database Operations

214

215

```javascript

216

// Example with a database library

217

const db = require('some-db-library');

218

const query = denodeify(db.query, true); // true for multiple return values

219

220

query('SELECT * FROM users WHERE id = ?', [userId])

221

.then(function(result) {

222

const rows = result[0];

223

const fields = result[1];

224

console.log('Query results:', rows);

225

});

226

```

227

228

## Browser Compatibility

229

230

denodeify works in all environments except IE 10 and below due to its use of function inheritance patterns. All properties of the original function remain available on the denodified function.